fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / jfs / jfs_extent.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *
4  *   This program is free software;  you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  *   the GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program;  if not, write to the Free Software
16  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include <linux/fs.h>
20 #include <linux/quotaops.h>
21 #include <linux/vs_dlimit.h>
22 #include "jfs_incore.h"
23 #include "jfs_inode.h"
24 #include "jfs_superblock.h"
25 #include "jfs_dmap.h"
26 #include "jfs_extent.h"
27 #include "jfs_debug.h"
28
29 /*
30  * forward references
31  */
32 static int extBalloc(struct inode *, s64, s64 *, s64 *);
33 #ifdef _NOTYET
34 static int extBrealloc(struct inode *, s64, s64, s64 *, s64 *);
35 #endif
36 static s64 extRoundDown(s64 nb);
37
38 #define DPD(a)          (printk("(a): %d\n",(a)))
39 #define DPC(a)          (printk("(a): %c\n",(a)))
40 #define DPL1(a)                                 \
41 {                                               \
42         if ((a) >> 32)                          \
43                 printk("(a): %x%08x  ",(a));    \
44         else                                    \
45                 printk("(a): %x  ",(a) << 32);  \
46 }
47 #define DPL(a)                                  \
48 {                                               \
49         if ((a) >> 32)                          \
50                 printk("(a): %x%08x\n",(a));    \
51         else                                    \
52                 printk("(a): %x\n",(a) << 32);  \
53 }
54
55 #define DPD1(a)         (printk("(a): %d  ",(a)))
56 #define DPX(a)          (printk("(a): %08x\n",(a)))
57 #define DPX1(a)         (printk("(a): %08x  ",(a)))
58 #define DPS(a)          (printk("%s\n",(a)))
59 #define DPE(a)          (printk("\nENTERING: %s\n",(a)))
60 #define DPE1(a)          (printk("\nENTERING: %s",(a)))
61 #define DPS1(a)         (printk("  %s  ",(a)))
62
63
64 /*
65  * NAME:        extAlloc()
66  *
67  * FUNCTION:    allocate an extent for a specified page range within a
68  *              file.
69  *
70  * PARAMETERS:
71  *      ip      - the inode of the file.
72  *      xlen    - requested extent length.
73  *      pno     - the starting page number with the file.
74  *      xp      - pointer to an xad.  on entry, xad describes an
75  *                extent that is used as an allocation hint if the
76  *                xaddr of the xad is non-zero.  on successful exit,
77  *                the xad describes the newly allocated extent.
78  *      abnr    - bool indicating whether the newly allocated extent
79  *                should be marked as allocated but not recorded.
80  *
81  * RETURN VALUES:
82  *      0       - success
83  *      -EIO    - i/o error.
84  *      -ENOSPC - insufficient disk resources.
85  */
86 int
87 extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, bool abnr)
88 {
89         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
90         s64 nxlen, nxaddr, xoff, hint, xaddr = 0;
91         int rc;
92         int xflag;
93
94         /* This blocks if we are low on resources */
95         txBeginAnon(ip->i_sb);
96
97         /* Avoid race with jfs_commit_inode() */
98         mutex_lock(&JFS_IP(ip)->commit_mutex);
99
100         /* validate extent length */
101         if (xlen > MAXXLEN)
102                 xlen = MAXXLEN;
103
104         /* get the page's starting extent offset */
105         xoff = pno << sbi->l2nbperpage;
106
107         /* check if an allocation hint was provided */
108         if ((hint = addressXAD(xp))) {
109                 /* get the size of the extent described by the hint */
110                 nxlen = lengthXAD(xp);
111
112                 /* check if the hint is for the portion of the file
113                  * immediately previous to the current allocation
114                  * request and if hint extent has the same abnr
115                  * value as the current request.  if so, we can
116                  * extend the hint extent to include the current
117                  * extent if we can allocate the blocks immediately
118                  * following the hint extent.
119                  */
120                 if (offsetXAD(xp) + nxlen == xoff &&
121                     abnr == ((xp->flag & XAD_NOTRECORDED) ? true : false))
122                         xaddr = hint + nxlen;
123
124                 /* adjust the hint to the last block of the extent */
125                 hint += (nxlen - 1);
126         }
127
128         /* allocate the disk blocks for the extent.  initially, extBalloc()
129          * will try to allocate disk blocks for the requested size (xlen).
130          * if this fails (xlen contiguous free blocks not avaliable), it'll
131          * try to allocate a smaller number of blocks (producing a smaller
132          * extent), with this smaller number of blocks consisting of the
133          * requested number of blocks rounded down to the next smaller
134          * power of 2 number (i.e. 16 -> 8).  it'll continue to round down
135          * and retry the allocation until the number of blocks to allocate
136          * is smaller than the number of blocks per page.
137          */
138         nxlen = xlen;
139         if ((rc = extBalloc(ip, hint ? hint : INOHINT(ip), &nxlen, &nxaddr))) {
140                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
141                 return (rc);
142         }
143
144         /* Allocate blocks to quota. */
145         if (DQUOT_ALLOC_BLOCK(ip, nxlen)) {
146                 dbFree(ip, nxaddr, (s64) nxlen);
147                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
148                 return -EDQUOT;
149         }
150
151         /* Allocate blocks to dlimit. */
152         if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
153                 DQUOT_FREE_BLOCK(ip, nxlen);
154                 dbFree(ip, nxaddr, (s64) nxlen);
155                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
156                 return -ENOSPC;
157         }
158
159         /* determine the value of the extent flag */
160         xflag = abnr ? XAD_NOTRECORDED : 0;
161
162         /* if we can extend the hint extent to cover the current request,
163          * extend it.  otherwise, insert a new extent to
164          * cover the current request.
165          */
166         if (xaddr && xaddr == nxaddr)
167                 rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
168         else
169                 rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
170
171         /* if the extend or insert failed,
172          * free the newly allocated blocks and return the error.
173          */
174         if (rc) {
175                 dbFree(ip, nxaddr, nxlen);
176                 DLIMIT_FREE_BLOCK(ip, nxlen);
177                 DQUOT_FREE_BLOCK(ip, nxlen);
178                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
179                 return (rc);
180         }
181
182         /* set the results of the extent allocation */
183         XADaddress(xp, nxaddr);
184         XADlength(xp, nxlen);
185         XADoffset(xp, xoff);
186         xp->flag = xflag;
187
188         mark_inode_dirty(ip);
189
190         mutex_unlock(&JFS_IP(ip)->commit_mutex);
191         /*
192          * COMMIT_SyncList flags an anonymous tlock on page that is on
193          * sync list.
194          * We need to commit the inode to get the page written disk.
195          */
196         if (test_and_clear_cflag(COMMIT_Synclist,ip))
197                 jfs_commit_inode(ip, 0);
198
199         return (0);
200 }
201
202
203 #ifdef _NOTYET
204 /*
205  * NAME:        extRealloc()
206  *
207  * FUNCTION:    extend the allocation of a file extent containing a
208  *              partial back last page.
209  *
210  * PARAMETERS:
211  *      ip      - the inode of the file.
212  *      cp      - cbuf for the partial backed last page.
213  *      xlen    - request size of the resulting extent.
214  *      xp      - pointer to an xad. on successful exit, the xad
215  *                describes the newly allocated extent.
216  *      abnr    - bool indicating whether the newly allocated extent
217  *                should be marked as allocated but not recorded.
218  *
219  * RETURN VALUES:
220  *      0       - success
221  *      -EIO    - i/o error.
222  *      -ENOSPC - insufficient disk resources.
223  */
224 int extRealloc(struct inode *ip, s64 nxlen, xad_t * xp, bool abnr)
225 {
226         struct super_block *sb = ip->i_sb;
227         s64 xaddr, xlen, nxaddr, delta, xoff;
228         s64 ntail, nextend, ninsert;
229         int rc, nbperpage = JFS_SBI(sb)->nbperpage;
230         int xflag;
231
232         /* This blocks if we are low on resources */
233         txBeginAnon(ip->i_sb);
234
235         mutex_lock(&JFS_IP(ip)->commit_mutex);
236         /* validate extent length */
237         if (nxlen > MAXXLEN)
238                 nxlen = MAXXLEN;
239
240         /* get the extend (partial) page's disk block address and
241          * number of blocks.
242          */
243         xaddr = addressXAD(xp);
244         xlen = lengthXAD(xp);
245         xoff = offsetXAD(xp);
246
247         /* if the extend page is abnr and if the request is for
248          * the extent to be allocated and recorded,
249          * make the page allocated and recorded.
250          */
251         if ((xp->flag & XAD_NOTRECORDED) && !abnr) {
252                 xp->flag = 0;
253                 if ((rc = xtUpdate(0, ip, xp)))
254                         goto exit;
255         }
256
257         /* try to allocated the request number of blocks for the
258          * extent.  dbRealloc() first tries to satisfy the request
259          * by extending the allocation in place. otherwise, it will
260          * try to allocate a new set of blocks large enough for the
261          * request.  in satisfying a request, dbReAlloc() may allocate
262          * less than what was request but will always allocate enough
263          * space as to satisfy the extend page.
264          */
265         if ((rc = extBrealloc(ip, xaddr, xlen, &nxlen, &nxaddr)))
266                 goto exit;
267
268         /* Allocat blocks to quota. */
269         if (DQUOT_ALLOC_BLOCK(ip, nxlen)) {
270                 dbFree(ip, nxaddr, (s64) nxlen);
271                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
272                 return -EDQUOT;
273         }
274         /* Allocate blocks to dlimit. */
275         if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
276                 DQUOT_FREE_BLOCK(ip, nxlen);
277                 dbFree(ip, nxaddr, (s64) nxlen);
278                 up(&JFS_IP(ip)->commit_sem);
279                 return -ENOSPC;
280         }
281
282         delta = nxlen - xlen;
283
284         /* check if the extend page is not abnr but the request is abnr
285          * and the allocated disk space is for more than one page.  if this
286          * is the case, there is a miss match of abnr between the extend page
287          * and the one or more pages following the extend page.  as a result,
288          * two extents will have to be manipulated. the first will be that
289          * of the extent of the extend page and will be manipulated thru
290          * an xtExtend() or an xtTailgate(), depending upon whether the
291          * disk allocation occurred as an inplace extension.  the second
292          * extent will be manipulated (created) through an xtInsert() and
293          * will be for the pages following the extend page.
294          */
295         if (abnr && (!(xp->flag & XAD_NOTRECORDED)) && (nxlen > nbperpage)) {
296                 ntail = nbperpage;
297                 nextend = ntail - xlen;
298                 ninsert = nxlen - nbperpage;
299
300                 xflag = XAD_NOTRECORDED;
301         } else {
302                 ntail = nxlen;
303                 nextend = delta;
304                 ninsert = 0;
305
306                 xflag = xp->flag;
307         }
308
309         /* if we were able to extend the disk allocation in place,
310          * extend the extent.  otherwise, move the extent to a
311          * new disk location.
312          */
313         if (xaddr == nxaddr) {
314                 /* extend the extent */
315                 if ((rc = xtExtend(0, ip, xoff + xlen, (int) nextend, 0))) {
316                         dbFree(ip, xaddr + xlen, delta);
317                         DLIMIT_FREE_BLOCK(ip, nxlen);
318                         DQUOT_FREE_BLOCK(ip, nxlen);
319                         goto exit;
320                 }
321         } else {
322                 /*
323                  * move the extent to a new location:
324                  *
325                  * xtTailgate() accounts for relocated tail extent;
326                  */
327                 if ((rc = xtTailgate(0, ip, xoff, (int) ntail, nxaddr, 0))) {
328                         dbFree(ip, nxaddr, nxlen);
329                         DLIMIT_FREE_BLOCK(ip, nxlen);
330                         DQUOT_FREE_BLOCK(ip, nxlen);
331                         goto exit;
332                 }
333         }
334
335
336         /* check if we need to also insert a new extent */
337         if (ninsert) {
338                 /* perform the insert.  if it fails, free the blocks
339                  * to be inserted and make it appear that we only did
340                  * the xtExtend() or xtTailgate() above.
341                  */
342                 xaddr = nxaddr + ntail;
343                 if (xtInsert (0, ip, xflag, xoff + ntail, (int) ninsert,
344                               &xaddr, 0)) {
345                         dbFree(ip, xaddr, (s64) ninsert);
346                         delta = nextend;
347                         nxlen = ntail;
348                         xflag = 0;
349                 }
350         }
351
352         /* set the return results */
353         XADaddress(xp, nxaddr);
354         XADlength(xp, nxlen);
355         XADoffset(xp, xoff);
356         xp->flag = xflag;
357
358         mark_inode_dirty(ip);
359 exit:
360         mutex_unlock(&JFS_IP(ip)->commit_mutex);
361         return (rc);
362 }
363 #endif                  /* _NOTYET */
364
365
366 /*
367  * NAME:        extHint()
368  *
369  * FUNCTION:    produce an extent allocation hint for a file offset.
370  *
371  * PARAMETERS:
372  *      ip      - the inode of the file.
373  *      offset  - file offset for which the hint is needed.
374  *      xp      - pointer to the xad that is to be filled in with
375  *                the hint.
376  *
377  * RETURN VALUES:
378  *      0       - success
379  *      -EIO    - i/o error.
380  */
381 int extHint(struct inode *ip, s64 offset, xad_t * xp)
382 {
383         struct super_block *sb = ip->i_sb;
384         struct xadlist xadl;
385         struct lxdlist lxdl;
386         lxd_t lxd;
387         s64 prev;
388         int rc, nbperpage = JFS_SBI(sb)->nbperpage;
389
390         /* init the hint as "no hint provided" */
391         XADaddress(xp, 0);
392
393         /* determine the starting extent offset of the page previous
394          * to the page containing the offset.
395          */
396         prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
397
398         /* if the offsets in the first page of the file,
399          * no hint provided.
400          */
401         if (prev < 0)
402                 return (0);
403
404         /* prepare to lookup the previous page's extent info */
405         lxdl.maxnlxd = 1;
406         lxdl.nlxd = 1;
407         lxdl.lxd = &lxd;
408         LXDoffset(&lxd, prev)
409             LXDlength(&lxd, nbperpage);
410
411         xadl.maxnxad = 1;
412         xadl.nxad = 0;
413         xadl.xad = xp;
414
415         /* perform the lookup */
416         if ((rc = xtLookupList(ip, &lxdl, &xadl, 0)))
417                 return (rc);
418
419         /* check if not extent exists for the previous page.
420          * this is possible for sparse files.
421          */
422         if (xadl.nxad == 0) {
423 //              assert(ISSPARSE(ip));
424                 return (0);
425         }
426
427         /* only preserve the abnr flag within the xad flags
428          * of the returned hint.
429          */
430         xp->flag &= XAD_NOTRECORDED;
431
432         if(xadl.nxad != 1 || lengthXAD(xp) != nbperpage) {
433                 jfs_error(ip->i_sb, "extHint: corrupt xtree");
434                 return -EIO;
435         }
436
437         return (0);
438 }
439
440
441 /*
442  * NAME:        extRecord()
443  *
444  * FUNCTION:    change a page with a file from not recorded to recorded.
445  *
446  * PARAMETERS:
447  *      ip      - inode of the file.
448  *      cp      - cbuf of the file page.
449  *
450  * RETURN VALUES:
451  *      0       - success
452  *      -EIO    - i/o error.
453  *      -ENOSPC - insufficient disk resources.
454  */
455 int extRecord(struct inode *ip, xad_t * xp)
456 {
457         int rc;
458
459         txBeginAnon(ip->i_sb);
460
461         mutex_lock(&JFS_IP(ip)->commit_mutex);
462
463         /* update the extent */
464         rc = xtUpdate(0, ip, xp);
465
466         mutex_unlock(&JFS_IP(ip)->commit_mutex);
467         return rc;
468 }
469
470
471 #ifdef _NOTYET
472 /*
473  * NAME:        extFill()
474  *
475  * FUNCTION:    allocate disk space for a file page that represents
476  *              a file hole.
477  *
478  * PARAMETERS:
479  *      ip      - the inode of the file.
480  *      cp      - cbuf of the file page represent the hole.
481  *
482  * RETURN VALUES:
483  *      0       - success
484  *      -EIO    - i/o error.
485  *      -ENOSPC - insufficient disk resources.
486  */
487 int extFill(struct inode *ip, xad_t * xp)
488 {
489         int rc, nbperpage = JFS_SBI(ip->i_sb)->nbperpage;
490         s64 blkno = offsetXAD(xp) >> ip->i_blkbits;
491
492 //      assert(ISSPARSE(ip));
493
494         /* initialize the extent allocation hint */
495         XADaddress(xp, 0);
496
497         /* allocate an extent to fill the hole */
498         if ((rc = extAlloc(ip, nbperpage, blkno, xp, false)))
499                 return (rc);
500
501         assert(lengthPXD(xp) == nbperpage);
502
503         return (0);
504 }
505 #endif                  /* _NOTYET */
506
507
508 /*
509  * NAME:        extBalloc()
510  *
511  * FUNCTION:    allocate disk blocks to form an extent.
512  *
513  *              initially, we will try to allocate disk blocks for the
514  *              requested size (nblocks).  if this fails (nblocks
515  *              contiguous free blocks not avaliable), we'll try to allocate
516  *              a smaller number of blocks (producing a smaller extent), with
517  *              this smaller number of blocks consisting of the requested
518  *              number of blocks rounded down to the next smaller power of 2
519  *              number (i.e. 16 -> 8).  we'll continue to round down and
520  *              retry the allocation until the number of blocks to allocate
521  *              is smaller than the number of blocks per page.
522  *
523  * PARAMETERS:
524  *      ip       - the inode of the file.
525  *      hint     - disk block number to be used as an allocation hint.
526  *      *nblocks - pointer to an s64 value.  on entry, this value specifies
527  *                 the desired number of block to be allocated. on successful
528  *                 exit, this value is set to the number of blocks actually
529  *                 allocated.
530  *      blkno    - pointer to a block address that is filled in on successful
531  *                 return with the starting block number of the newly
532  *                 allocated block range.
533  *
534  * RETURN VALUES:
535  *      0       - success
536  *      -EIO    - i/o error.
537  *      -ENOSPC - insufficient disk resources.
538  */
539 static int
540 extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
541 {
542         struct jfs_inode_info *ji = JFS_IP(ip);
543         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
544         s64 nb, nblks, daddr, max;
545         int rc, nbperpage = sbi->nbperpage;
546         struct bmap *bmp = sbi->bmap;
547         int ag;
548
549         /* get the number of blocks to initially attempt to allocate.
550          * we'll first try the number of blocks requested unless this
551          * number is greater than the maximum number of contiguous free
552          * blocks in the map. in that case, we'll start off with the
553          * maximum free.
554          */
555         max = (s64) 1 << bmp->db_maxfreebud;
556         if (*nblocks >= max && *nblocks > nbperpage)
557                 nb = nblks = (max > nbperpage) ? max : nbperpage;
558         else
559                 nb = nblks = *nblocks;
560
561         /* try to allocate blocks */
562         while ((rc = dbAlloc(ip, hint, nb, &daddr)) != 0) {
563                 /* if something other than an out of space error,
564                  * stop and return this error.
565                  */
566                 if (rc != -ENOSPC)
567                         return (rc);
568
569                 /* decrease the allocation request size */
570                 nb = min(nblks, extRoundDown(nb));
571
572                 /* give up if we cannot cover a page */
573                 if (nb < nbperpage)
574                         return (rc);
575         }
576
577         *nblocks = nb;
578         *blkno = daddr;
579
580         if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
581                 ag = BLKTOAG(daddr, sbi);
582                 spin_lock_irq(&ji->ag_lock);
583                 if (ji->active_ag == -1) {
584                         atomic_inc(&bmp->db_active[ag]);
585                         ji->active_ag = ag;
586                 } else if (ji->active_ag != ag) {
587                         atomic_dec(&bmp->db_active[ji->active_ag]);
588                         atomic_inc(&bmp->db_active[ag]);
589                         ji->active_ag = ag;
590                 }
591                 spin_unlock_irq(&ji->ag_lock);
592         }
593
594         return (0);
595 }
596
597
598 #ifdef _NOTYET
599 /*
600  * NAME:        extBrealloc()
601  *
602  * FUNCTION:    attempt to extend an extent's allocation.
603  *
604  *              Initially, we will try to extend the extent's allocation
605  *              in place.  If this fails, we'll try to move the extent
606  *              to a new set of blocks.  If moving the extent, we initially
607  *              will try to allocate disk blocks for the requested size
608  *              (newnblks).  if this fails (new contiguous free blocks not
609  *              avaliable), we'll try to allocate a smaller number of
610  *              blocks (producing a smaller extent), with this smaller
611  *              number of blocks consisting of the requested number of
612  *              blocks rounded down to the next smaller power of 2
613  *              number (i.e. 16 -> 8).  We'll continue to round down and
614  *              retry the allocation until the number of blocks to allocate
615  *              is smaller than the number of blocks per page.
616  *
617  * PARAMETERS:
618  *      ip       - the inode of the file.
619  *      blkno    - starting block number of the extents current allocation.
620  *      nblks    - number of blocks within the extents current allocation.
621  *      newnblks - pointer to a s64 value.  on entry, this value is the
622  *                 the new desired extent size (number of blocks).  on
623  *                 successful exit, this value is set to the extent's actual
624  *                 new size (new number of blocks).
625  *      newblkno - the starting block number of the extents new allocation.
626  *
627  * RETURN VALUES:
628  *      0       - success
629  *      -EIO    - i/o error.
630  *      -ENOSPC - insufficient disk resources.
631  */
632 static int
633 extBrealloc(struct inode *ip,
634             s64 blkno, s64 nblks, s64 * newnblks, s64 * newblkno)
635 {
636         int rc;
637
638         /* try to extend in place */
639         if ((rc = dbExtend(ip, blkno, nblks, *newnblks - nblks)) == 0) {
640                 *newblkno = blkno;
641                 return (0);
642         } else {
643                 if (rc != -ENOSPC)
644                         return (rc);
645         }
646
647         /* in place extension not possible.
648          * try to move the extent to a new set of blocks.
649          */
650         return (extBalloc(ip, blkno, newnblks, newblkno));
651 }
652 #endif                  /* _NOTYET */
653
654
655 /*
656  * NAME:        extRoundDown()
657  *
658  * FUNCTION:    round down a specified number of blocks to the next
659  *              smallest power of 2 number.
660  *
661  * PARAMETERS:
662  *      nb      - the inode of the file.
663  *
664  * RETURN VALUES:
665  *      next smallest power of 2 number.
666  */
667 static s64 extRoundDown(s64 nb)
668 {
669         int i;
670         u64 m, k;
671
672         for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
673                 if (m & nb)
674                         break;
675         }
676
677         i = 63 - i;
678         k = (u64) 1 << i;
679         k = ((k - 1) & nb) ? k : k >> 1;
680
681         return (k);
682 }