1
2
3
4
5
6#include <linux/fs.h>
7#include <linux/buffer_head.h>
8#include <linux/quotaops.h>
9#include "jfs_incore.h"
10#include "jfs_filsys.h"
11#include "jfs_metapage.h"
12#include "jfs_dinode.h"
13#include "jfs_imap.h"
14#include "jfs_dmap.h"
15#include "jfs_superblock.h"
16#include "jfs_txnmgr.h"
17#include "jfs_debug.h"
18
19#define BITSPERPAGE (PSIZE << 3)
20#define L2MEGABYTE 20
21#define MEGABYTE (1 << L2MEGABYTE)
22#define MEGABYTE32 (MEGABYTE << 5)
23
24
25#define BLKTODMAPN(b)\
26 (((b) >> 13) + ((b) >> 23) + ((b) >> 33) + 3 + 1)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49int jfs_extendfs(struct super_block *sb, s64 newLVSize, int newLogSize)
50{
51 int rc = 0;
52 struct jfs_sb_info *sbi = JFS_SBI(sb);
53 struct inode *ipbmap = sbi->ipbmap;
54 struct inode *ipbmap2;
55 struct inode *ipimap = sbi->ipimap;
56 struct jfs_log *log = sbi->log;
57 struct bmap *bmp = sbi->bmap;
58 s64 newLogAddress, newFSCKAddress;
59 int newFSCKSize;
60 s64 newMapSize = 0, mapSize;
61 s64 XAddress, XSize, nblocks, xoff, xaddr, t64;
62 s64 oldLVSize;
63 s64 newFSSize;
64 s64 VolumeSize;
65 int newNpages = 0, nPages, newPage, xlen, t32;
66 int tid;
67 int log_formatted = 0;
68 struct inode *iplist[1];
69 struct jfs_superblock *j_sb, *j_sb2;
70 s64 old_agsize;
71 int agsizechanged = 0;
72 struct buffer_head *bh, *bh2;
73
74
75
76 if (sbi->mntflag & JFS_INLINELOG)
77 oldLVSize = addressPXD(&sbi->logpxd) + lengthPXD(&sbi->logpxd);
78 else
79 oldLVSize = addressPXD(&sbi->fsckpxd) +
80 lengthPXD(&sbi->fsckpxd);
81
82 if (oldLVSize >= newLVSize) {
83 printk(KERN_WARNING
84 "jfs_extendfs: volume hasn't grown, returning\n");
85 goto out;
86 }
87
88 VolumeSize = i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits;
89
90 if (VolumeSize) {
91 if (newLVSize > VolumeSize) {
92 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
93 rc = -EINVAL;
94 goto out;
95 }
96 } else {
97
98 bh = sb_bread(sb, newLVSize - 1);
99 if (!bh) {
100 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
101 rc = -EINVAL;
102 goto out;
103 }
104 bforget(bh);
105 }
106
107
108
109 if (isReadOnly(ipbmap)) {
110 printk(KERN_WARNING "jfs_extendfs: read-only file system\n");
111 rc = -EROFS;
112 goto out;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 if ((sbi->mntflag & JFS_INLINELOG)) {
126 if (newLogSize == 0) {
127
128
129
130
131 newLogSize = newLVSize >> 8;
132 t32 = (1 << (20 - sbi->l2bsize)) - 1;
133 newLogSize = (newLogSize + t32) & ~t32;
134 newLogSize =
135 min(newLogSize, MEGABYTE32 >> sbi->l2bsize);
136 } else {
137
138
139
140
141
142
143 newLogSize = (newLogSize * MEGABYTE) >> sbi->l2bsize;
144 }
145
146 } else
147 newLogSize = 0;
148
149 newLogAddress = newLVSize - newLogSize;
150
151
152
153
154
155
156
157
158
159
160
161 t64 = ((newLVSize - newLogSize + BPERDMAP - 1) >> L2BPERDMAP)
162 << L2BPERDMAP;
163 t32 = DIV_ROUND_UP(t64, BITSPERPAGE) + 1 + 50;
164 newFSCKSize = t32 << sbi->l2nbperpage;
165 newFSCKAddress = newLogAddress - newFSCKSize;
166
167
168
169
170 newFSSize = newLVSize - newLogSize - newFSCKSize;
171
172
173 if (newFSSize < bmp->db_mapsize) {
174 rc = -EINVAL;
175 goto out;
176 }
177
178
179
180
181
182
183 if ((sbi->mntflag & JFS_INLINELOG) && (newLogAddress > oldLVSize)) {
184 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
185 goto out;
186 log_formatted = 1;
187 }
188
189
190
191
192
193
194
195
196
197
198 txQuiesce(sb);
199
200
201 sbi->direct_inode->i_size = i_size_read(sb->s_bdev->bd_inode);
202
203 if (sbi->mntflag & JFS_INLINELOG) {
204
205
206
207 lmLogShutdown(log);
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 if ((rc = readSuper(sb, &bh)))
225 goto error_out;
226 j_sb = (struct jfs_superblock *)bh->b_data;
227
228
229 j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
230 j_sb->s_xsize = cpu_to_le64(newFSSize);
231 PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
232 PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
233 PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
234 PXDlength(&j_sb->s_xlogpxd, newLogSize);
235
236
237 mark_buffer_dirty(bh);
238 sync_dirty_buffer(bh);
239 brelse(bh);
240
241
242
243
244
245
246
247 if (!log_formatted)
248 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
249 goto error_out;
250
251
252
253
254 log->base = newLogAddress;
255 log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
256 if ((rc = lmLogInit(log)))
257 goto error_out;
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 newMapSize = newFSSize;
284
285
286
287
288 t64 = (newMapSize - 1) + BPERDMAP;
289 newNpages = BLKTODMAPN(t64) + 1;
290
291
292
293
294
295
296
297
298 extendBmap:
299
300 mapSize = bmp->db_mapsize;
301 XAddress = mapSize;
302 XSize = newMapSize - mapSize;
303 old_agsize = bmp->db_agsize;
304
305
306 t64 = dbMapFileSizeToMapSize(ipbmap);
307 if (mapSize > t64) {
308 printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
309 (long long) mapSize, (long long) t64);
310 rc = -EIO;
311 goto error_out;
312 }
313 nblocks = min(t64 - mapSize, XSize);
314
315
316
317
318
319
320
321
322 if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
323 goto error_out;
324
325 agsizechanged |= (bmp->db_agsize != old_agsize);
326
327
328
329
330
331
332 XSize -= nblocks;
333
334
335
336
337
338
339
340
341
342 nPages = ipbmap->i_size >> L2PSIZE;
343
344
345 if (nPages == newNpages)
346 goto finalizeBmap;
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369 rc = filemap_fdatawait(ipbmap->i_mapping);
370 if (rc)
371 goto error_out;
372
373 rc = filemap_write_and_wait(ipbmap->i_mapping);
374 if (rc)
375 goto error_out;
376
377 diWriteSpecial(ipbmap, 0);
378
379 newPage = nPages;
380 xoff = newPage << sbi->l2nbperpage;
381 xlen = (newNpages - nPages) << sbi->l2nbperpage;
382 xlen = min(xlen, (int) nblocks) & ~(sbi->nbperpage - 1);
383 xaddr = XAddress;
384
385 tid = txBegin(sb, COMMIT_FORCE);
386
387 if ((rc = xtAppend(tid, ipbmap, 0, xoff, nblocks, &xlen, &xaddr, 0))) {
388 txEnd(tid);
389 goto error_out;
390 }
391
392 ipbmap->i_size += xlen << sbi->l2bsize;
393 inode_add_bytes(ipbmap, xlen << sbi->l2bsize);
394
395 iplist[0] = ipbmap;
396 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
397
398 txEnd(tid);
399
400 if (rc)
401 goto error_out;
402
403
404
405
406
407
408
409
410
411
412
413
414 if (XSize)
415 goto extendBmap;
416
417 finalizeBmap:
418
419 dbFinalizeBmap(ipbmap);
420
421
422
423
424
425
426
427
428
429
430
431
432 if (agsizechanged) {
433 if ((rc = diExtendFS(ipimap, ipbmap)))
434 goto error_out;
435
436
437 if ((rc = diSync(ipimap)))
438 goto error_out;
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 if ((rc = dbSync(ipbmap)))
461 goto error_out;
462
463
464
465
466
467 ipbmap2 = diReadSpecial(sb, BMAP_I, 1);
468 if (ipbmap2 == NULL) {
469 printk(KERN_ERR "jfs_extendfs: diReadSpecial(bmap) failed\n");
470 goto error_out;
471 }
472 memcpy(&JFS_IP(ipbmap2)->i_xtroot, &JFS_IP(ipbmap)->i_xtroot, 288);
473 ipbmap2->i_size = ipbmap->i_size;
474 ipbmap2->i_blocks = ipbmap->i_blocks;
475
476 diWriteSpecial(ipbmap2, 1);
477 diFreeSpecial(ipbmap2);
478
479
480
481
482 if ((rc = readSuper(sb, &bh)))
483 goto error_out;
484 j_sb = (struct jfs_superblock *)bh->b_data;
485
486
487 j_sb->s_state &= cpu_to_le32(~FM_EXTENDFS);
488 j_sb->s_size = cpu_to_le64(bmp->db_mapsize <<
489 le16_to_cpu(j_sb->s_l2bfactor));
490 j_sb->s_agsize = cpu_to_le32(bmp->db_agsize);
491
492
493 if (sbi->mntflag & JFS_INLINELOG) {
494 PXDaddress(&(j_sb->s_logpxd), newLogAddress);
495 PXDlength(&(j_sb->s_logpxd), newLogSize);
496 }
497
498
499 j_sb->s_logserial = cpu_to_le32(log->serial);
500
501
502 PXDaddress(&(j_sb->s_fsckpxd), newFSCKAddress);
503 PXDlength(&(j_sb->s_fsckpxd), newFSCKSize);
504 j_sb->s_fscklog = 1;
505
506
507
508 bh2 = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
509 if (bh2) {
510 j_sb2 = (struct jfs_superblock *)bh2->b_data;
511 memcpy(j_sb2, j_sb, sizeof (struct jfs_superblock));
512
513 mark_buffer_dirty(bh);
514 sync_dirty_buffer(bh2);
515 brelse(bh2);
516 }
517
518
519 mark_buffer_dirty(bh);
520 sync_dirty_buffer(bh);
521 brelse(bh);
522
523 goto resume;
524
525 error_out:
526 jfs_error(sb, "\n");
527
528 resume:
529
530
531
532 txResume(sb);
533
534 out:
535 return rc;
536}
537