Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.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    - boolean_t 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, boolean_t 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         /* Allocate blocks to dlimit. */
151         if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
152                 DQUOT_FREE_BLOCK(ip, nxlen);
153                 dbFree(ip, nxaddr, (s64) nxlen);
154                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
155                 return -ENOSPC;
156         }
157
158         /* determine the value of the extent flag */
159         xflag = (abnr == TRUE) ? XAD_NOTRECORDED : 0;
160
161         /* if we can extend the hint extent to cover the current request, 
162          * extend it.  otherwise, insert a new extent to
163          * cover the current request.
164          */
165         if (xaddr && xaddr == nxaddr)
166                 rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
167         else
168                 rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
169
170         /* if the extend or insert failed, 
171          * free the newly allocated blocks and return the error.
172          */
173         if (rc) {
174                 dbFree(ip, nxaddr, nxlen);
175                 DLIMIT_FREE_BLOCK(ip, nxlen);
176                 DQUOT_FREE_BLOCK(ip, nxlen);
177                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
178                 return (rc);
179         }
180
181         /* set the results of the extent allocation */
182         XADaddress(xp, nxaddr);
183         XADlength(xp, nxlen);
184         XADoffset(xp, xoff);
185         xp->flag = xflag;
186
187         mark_inode_dirty(ip);
188
189         mutex_unlock(&JFS_IP(ip)->commit_mutex);
190         /*
191          * COMMIT_SyncList flags an anonymous tlock on page that is on
192          * sync list.
193          * We need to commit the inode to get the page written disk.
194          */
195         if (test_and_clear_cflag(COMMIT_Synclist,ip))
196                 jfs_commit_inode(ip, 0);
197
198         return (0);
199 }
200
201
202 #ifdef _NOTYET
203 /*
204  * NAME:        extRealloc()
205  *
206  * FUNCTION:    extend the allocation of a file extent containing a
207  *              partial back last page.
208  *
209  * PARAMETERS:
210  *      ip      - the inode of the file.
211  *      cp      - cbuf for the partial backed last page.
212  *      xlen    - request size of the resulting extent.
213  *      xp      - pointer to an xad. on successful exit, the xad
214  *                describes the newly allocated extent.
215  *      abnr    - boolean_t indicating whether the newly allocated extent
216  *                should be marked as allocated but not recorded.
217  *
218  * RETURN VALUES:
219  *      0       - success
220  *      -EIO    - i/o error.
221  *      -ENOSPC - insufficient disk resources.
222  */
223 int extRealloc(struct inode *ip, s64 nxlen, xad_t * xp, boolean_t abnr)
224 {
225         struct super_block *sb = ip->i_sb;
226         s64 xaddr, xlen, nxaddr, delta, xoff;
227         s64 ntail, nextend, ninsert;
228         int rc, nbperpage = JFS_SBI(sb)->nbperpage;
229         int xflag;
230
231         /* This blocks if we are low on resources */
232         txBeginAnon(ip->i_sb);
233
234         mutex_lock(&JFS_IP(ip)->commit_mutex);
235         /* validate extent length */
236         if (nxlen > MAXXLEN)
237                 nxlen = MAXXLEN;
238
239         /* get the extend (partial) page's disk block address and
240          * number of blocks.
241          */
242         xaddr = addressXAD(xp);
243         xlen = lengthXAD(xp);
244         xoff = offsetXAD(xp);
245
246         /* if the extend page is abnr and if the request is for
247          * the extent to be allocated and recorded, 
248          * make the page allocated and recorded.
249          */
250         if ((xp->flag & XAD_NOTRECORDED) && !abnr) {
251                 xp->flag = 0;
252                 if ((rc = xtUpdate(0, ip, xp)))
253                         goto exit;
254         }
255
256         /* try to allocated the request number of blocks for the
257          * extent.  dbRealloc() first tries to satisfy the request
258          * by extending the allocation in place. otherwise, it will
259          * try to allocate a new set of blocks large enough for the
260          * request.  in satisfying a request, dbReAlloc() may allocate
261          * less than what was request but will always allocate enough
262          * space as to satisfy the extend page.
263          */
264         if ((rc = extBrealloc(ip, xaddr, xlen, &nxlen, &nxaddr)))
265                 goto exit;
266
267         /* Allocat blocks to quota. */
268         if (DQUOT_ALLOC_BLOCK(ip, nxlen)) {
269                 dbFree(ip, nxaddr, (s64) nxlen);
270                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
271                 return -EDQUOT;
272         }
273         /* Allocate blocks to dlimit. */
274         if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
275                 DQUOT_FREE_BLOCK(ip, nxlen);
276                 dbFree(ip, nxaddr, (s64) nxlen);
277                 up(&JFS_IP(ip)->commit_sem);
278                 return -ENOSPC;
279         }
280
281         delta = nxlen - xlen;
282
283         /* check if the extend page is not abnr but the request is abnr
284          * and the allocated disk space is for more than one page.  if this
285          * is the case, there is a miss match of abnr between the extend page
286          * and the one or more pages following the extend page.  as a result,
287          * two extents will have to be manipulated. the first will be that
288          * of the extent of the extend page and will be manipulated thru
289          * an xtExtend() or an xtTailgate(), depending upon whether the
290          * disk allocation occurred as an inplace extension.  the second
291          * extent will be manipulated (created) through an xtInsert() and
292          * will be for the pages following the extend page.
293          */
294         if (abnr && (!(xp->flag & XAD_NOTRECORDED)) && (nxlen > nbperpage)) {
295                 ntail = nbperpage;
296                 nextend = ntail - xlen;
297                 ninsert = nxlen - nbperpage;
298
299                 xflag = XAD_NOTRECORDED;
300         } else {
301                 ntail = nxlen;
302                 nextend = delta;
303                 ninsert = 0;
304
305                 xflag = xp->flag;
306         }
307
308         /* if we were able to extend the disk allocation in place,
309          * extend the extent.  otherwise, move the extent to a
310          * new disk location.
311          */
312         if (xaddr == nxaddr) {
313                 /* extend the extent */
314                 if ((rc = xtExtend(0, ip, xoff + xlen, (int) nextend, 0))) {
315                         dbFree(ip, xaddr + xlen, delta);
316                         DLIMIT_FREE_BLOCK(ip, nxlen);
317                         DQUOT_FREE_BLOCK(ip, nxlen);
318                         goto exit;
319                 }
320         } else {
321                 /*
322                  * move the extent to a new location:
323                  *
324                  * xtTailgate() accounts for relocated tail extent;
325                  */
326                 if ((rc = xtTailgate(0, ip, xoff, (int) ntail, nxaddr, 0))) {
327                         dbFree(ip, nxaddr, nxlen);
328                         DLIMIT_FREE_BLOCK(ip, nxlen);
329                         DQUOT_FREE_BLOCK(ip, nxlen);
330                         goto exit;
331                 }
332         }
333
334
335         /* check if we need to also insert a new extent */
336         if (ninsert) {
337                 /* perform the insert.  if it fails, free the blocks
338                  * to be inserted and make it appear that we only did
339                  * the xtExtend() or xtTailgate() above.
340                  */
341                 xaddr = nxaddr + ntail;
342                 if (xtInsert (0, ip, xflag, xoff + ntail, (int) ninsert,
343                               &xaddr, 0)) {
344                         dbFree(ip, xaddr, (s64) ninsert);
345                         delta = nextend;
346                         nxlen = ntail;
347                         xflag = 0;
348                 }
349         }
350
351         /* set the return results */
352         XADaddress(xp, nxaddr);
353         XADlength(xp, nxlen);
354         XADoffset(xp, xoff);
355         xp->flag = xflag;
356
357         mark_inode_dirty(ip);
358 exit:
359         mutex_unlock(&JFS_IP(ip)->commit_mutex);
360         return (rc);
361 }
362 #endif                  /* _NOTYET */
363
364
365 /*
366  * NAME:        extHint()
367  *
368  * FUNCTION:    produce an extent allocation hint for a file offset.
369  *
370  * PARAMETERS:
371  *      ip      - the inode of the file.
372  *      offset  - file offset for which the hint is needed.
373  *      xp      - pointer to the xad that is to be filled in with
374  *                the hint.
375  *
376  * RETURN VALUES:
377  *      0       - success
378  *      -EIO    - i/o error.
379  */
380 int extHint(struct inode *ip, s64 offset, xad_t * xp)
381 {
382         struct super_block *sb = ip->i_sb;
383         struct xadlist xadl;
384         struct lxdlist lxdl;
385         lxd_t lxd;
386         s64 prev;
387         int rc, nbperpage = JFS_SBI(sb)->nbperpage;
388
389         /* init the hint as "no hint provided" */
390         XADaddress(xp, 0);
391
392         /* determine the starting extent offset of the page previous
393          * to the page containing the offset.
394          */
395         prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
396
397         /* if the offsets in the first page of the file,
398          * no hint provided.
399          */
400         if (prev < 0)
401                 return (0);
402
403         /* prepare to lookup the previous page's extent info */
404         lxdl.maxnlxd = 1;
405         lxdl.nlxd = 1;
406         lxdl.lxd = &lxd;
407         LXDoffset(&lxd, prev)
408             LXDlength(&lxd, nbperpage);
409
410         xadl.maxnxad = 1;
411         xadl.nxad = 0;
412         xadl.xad = xp;
413
414         /* perform the lookup */
415         if ((rc = xtLookupList(ip, &lxdl, &xadl, 0)))
416                 return (rc);
417
418         /* check if not extent exists for the previous page.  
419          * this is possible for sparse files.
420          */
421         if (xadl.nxad == 0) {
422 //              assert(ISSPARSE(ip));
423                 return (0);
424         }
425
426         /* only preserve the abnr flag within the xad flags
427          * of the returned hint.
428          */
429         xp->flag &= XAD_NOTRECORDED;
430
431         if(xadl.nxad != 1 || lengthXAD(xp) != nbperpage) {          
432                 jfs_error(ip->i_sb, "extHint: corrupt xtree");
433                 return -EIO;
434         }
435
436         return (0);
437 }
438
439
440 /*
441  * NAME:        extRecord()
442  *
443  * FUNCTION:    change a page with a file from not recorded to recorded.
444  *
445  * PARAMETERS:
446  *      ip      - inode of the file.
447  *      cp      - cbuf of the file page.
448  *
449  * RETURN VALUES:
450  *      0       - success
451  *      -EIO    - i/o error.
452  *      -ENOSPC - insufficient disk resources.
453  */
454 int extRecord(struct inode *ip, xad_t * xp)
455 {
456         int rc;
457
458         txBeginAnon(ip->i_sb);
459
460         mutex_lock(&JFS_IP(ip)->commit_mutex);
461
462         /* update the extent */
463         rc = xtUpdate(0, ip, xp);
464
465         mutex_unlock(&JFS_IP(ip)->commit_mutex);
466         return rc;
467 }
468
469
470 #ifdef _NOTYET
471 /*
472  * NAME:        extFill()
473  *
474  * FUNCTION:    allocate disk space for a file page that represents
475  *              a file hole.
476  *
477  * PARAMETERS:
478  *      ip      - the inode of the file.
479  *      cp      - cbuf of the file page represent the hole.
480  *
481  * RETURN VALUES:
482  *      0       - success
483  *      -EIO    - i/o error.
484  *      -ENOSPC - insufficient disk resources.
485  */
486 int extFill(struct inode *ip, xad_t * xp)
487 {
488         int rc, nbperpage = JFS_SBI(ip->i_sb)->nbperpage;
489         s64 blkno = offsetXAD(xp) >> ip->i_blkbits;
490
491 //      assert(ISSPARSE(ip));
492
493         /* initialize the extent allocation hint */
494         XADaddress(xp, 0);
495
496         /* allocate an extent to fill the hole */
497         if ((rc = extAlloc(ip, nbperpage, blkno, xp, FALSE)))
498                 return (rc);
499
500         assert(lengthPXD(xp) == nbperpage);
501
502         return (0);
503 }
504 #endif                  /* _NOTYET */
505
506
507 /*
508  * NAME:        extBalloc()
509  *
510  * FUNCTION:    allocate disk blocks to form an extent.
511  *
512  *              initially, we will try to allocate disk blocks for the
513  *              requested size (nblocks).  if this fails (nblocks 
514  *              contiguous free blocks not avaliable), we'll try to allocate
515  *              a smaller number of blocks (producing a smaller extent), with
516  *              this smaller number of blocks consisting of the requested
517  *              number of blocks rounded down to the next smaller power of 2
518  *              number (i.e. 16 -> 8).  we'll continue to round down and
519  *              retry the allocation until the number of blocks to allocate
520  *              is smaller than the number of blocks per page.
521  *              
522  * PARAMETERS:
523  *      ip       - the inode of the file.
524  *      hint     - disk block number to be used as an allocation hint.
525  *      *nblocks - pointer to an s64 value.  on entry, this value specifies
526  *                 the desired number of block to be allocated. on successful
527  *                 exit, this value is set to the number of blocks actually
528  *                 allocated.
529  *      blkno    - pointer to a block address that is filled in on successful
530  *                 return with the starting block number of the newly 
531  *                 allocated block range.
532  *
533  * RETURN VALUES:
534  *      0       - success
535  *      -EIO    - i/o error.
536  *      -ENOSPC - insufficient disk resources.
537  */
538 static int
539 extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
540 {
541         struct jfs_inode_info *ji = JFS_IP(ip);
542         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
543         s64 nb, nblks, daddr, max;
544         int rc, nbperpage = sbi->nbperpage;
545         struct bmap *bmp = sbi->bmap;
546         int ag;
547
548         /* get the number of blocks to initially attempt to allocate.
549          * we'll first try the number of blocks requested unless this
550          * number is greater than the maximum number of contiguous free
551          * blocks in the map. in that case, we'll start off with the 
552          * maximum free.
553          */
554         max = (s64) 1 << bmp->db_maxfreebud;
555         if (*nblocks >= max && *nblocks > nbperpage)
556                 nb = nblks = (max > nbperpage) ? max : nbperpage;
557         else
558                 nb = nblks = *nblocks;
559
560         /* try to allocate blocks */
561         while ((rc = dbAlloc(ip, hint, nb, &daddr)) != 0) {
562                 /* if something other than an out of space error,
563                  * stop and return this error.
564                  */
565                 if (rc != -ENOSPC)
566                         return (rc);
567
568                 /* decrease the allocation request size */
569                 nb = min(nblks, extRoundDown(nb));
570
571                 /* give up if we cannot cover a page */
572                 if (nb < nbperpage)
573                         return (rc);
574         }
575
576         *nblocks = nb;
577         *blkno = daddr;
578
579         if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
580                 ag = BLKTOAG(daddr, sbi);
581                 spin_lock_irq(&ji->ag_lock);
582                 if (ji->active_ag == -1) {
583                         atomic_inc(&bmp->db_active[ag]);
584                         ji->active_ag = ag;
585                 } else if (ji->active_ag != ag) {
586                         atomic_dec(&bmp->db_active[ji->active_ag]);
587                         atomic_inc(&bmp->db_active[ag]);
588                         ji->active_ag = ag;
589                 }
590                 spin_unlock_irq(&ji->ag_lock);
591         }
592
593         return (0);
594 }
595
596
597 #ifdef _NOTYET
598 /*
599  * NAME:        extBrealloc()
600  *
601  * FUNCTION:    attempt to extend an extent's allocation.
602  *
603  *              initially, we will try to extend the extent's allocation
604  *              in place.  if this fails, we'll try to move the extent
605  *              to a new set of blocks. if moving the extent, we initially
606  *              will try to allocate disk blocks for the requested size
607  *              (nnew).  if this fails  (new contiguous free blocks not
608  *              avaliable), we'll try  to allocate a smaller number of
609  *              blocks (producing a smaller extent), with this smaller
610  *              number of blocks consisting of the requested number of
611  *              blocks rounded down to the next smaller power of 2
612  *              number (i.e. 16 -> 8).  we'll continue to round down and
613  *              retry the allocation until the number of blocks to allocate
614  *              is smaller than the number of blocks per page.
615  *              
616  * PARAMETERS:
617  *      ip       - the inode of the file.
618  *      blkno    - starting block number of the extents current allocation.
619  *      nblks    - number of blocks within the extents current allocation.
620  *      newnblks - pointer to a s64 value.  on entry, this value is the
621  *                 the new desired extent size (number of blocks).  on
622  *                 successful exit, this value is set to the extent's actual
623  *                 new size (new number of blocks).
624  *      newblkno - the starting block number of the extents new allocation.
625  *
626  * RETURN VALUES:
627  *      0       - success
628  *      -EIO    - i/o error.
629  *      -ENOSPC - insufficient disk resources.
630  */
631 static int
632 extBrealloc(struct inode *ip,
633             s64 blkno, s64 nblks, s64 * newnblks, s64 * newblkno)
634 {
635         int rc;
636
637         /* try to extend in place */
638         if ((rc = dbExtend(ip, blkno, nblks, *newnblks - nblks)) == 0) {
639                 *newblkno = blkno;
640                 return (0);
641         } else {
642                 if (rc != -ENOSPC)
643                         return (rc);
644         }
645
646         /* in place extension not possible.  
647          * try to move the extent to a new set of blocks.
648          */
649         return (extBalloc(ip, blkno, newnblks, newblkno));
650 }
651 #endif                  /* _NOTYET */
652
653
654 /*
655  * NAME:        extRoundDown()
656  *
657  * FUNCTION:    round down a specified number of blocks to the next
658  *              smallest power of 2 number.
659  *
660  * PARAMETERS:
661  *      nb      - the inode of the file.
662  *
663  * RETURN VALUES:
664  *      next smallest power of 2 number.
665  */
666 static s64 extRoundDown(s64 nb)
667 {
668         int i;
669         u64 m, k;
670
671         for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
672                 if (m & nb)
673                         break;
674         }
675
676         i = 63 - i;
677         k = (u64) 1 << i;
678         k = ((k - 1) & nb) ? k : k >> 1;
679
680         return (k);
681 }