powerpc/spufs: convert userns uid/gid mount options to kuid/kgid
[linux-block.git] / fs / xfs / xfs_dir2.c
1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * 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 the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_bmap.h"
34 #include "xfs_dir2_format.h"
35 #include "xfs_dir2.h"
36 #include "xfs_dir2_priv.h"
37 #include "xfs_error.h"
38 #include "xfs_trace.h"
39
40 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2};
41
42 /*
43  * ASCII case-insensitive (ie. A-Z) support for directories that was
44  * used in IRIX.
45  */
46 STATIC xfs_dahash_t
47 xfs_ascii_ci_hashname(
48         struct xfs_name *name)
49 {
50         xfs_dahash_t    hash;
51         int             i;
52
53         for (i = 0, hash = 0; i < name->len; i++)
54                 hash = tolower(name->name[i]) ^ rol32(hash, 7);
55
56         return hash;
57 }
58
59 STATIC enum xfs_dacmp
60 xfs_ascii_ci_compname(
61         struct xfs_da_args *args,
62         const unsigned char *name,
63         int             len)
64 {
65         enum xfs_dacmp  result;
66         int             i;
67
68         if (args->namelen != len)
69                 return XFS_CMP_DIFFERENT;
70
71         result = XFS_CMP_EXACT;
72         for (i = 0; i < len; i++) {
73                 if (args->name[i] == name[i])
74                         continue;
75                 if (tolower(args->name[i]) != tolower(name[i]))
76                         return XFS_CMP_DIFFERENT;
77                 result = XFS_CMP_CASE;
78         }
79
80         return result;
81 }
82
83 static struct xfs_nameops xfs_ascii_ci_nameops = {
84         .hashname       = xfs_ascii_ci_hashname,
85         .compname       = xfs_ascii_ci_compname,
86 };
87
88 void
89 xfs_dir_mount(
90         xfs_mount_t     *mp)
91 {
92         ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb));
93         ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
94                XFS_MAX_BLOCKSIZE);
95         mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
96         mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
97         mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
98         mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
99         mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
100         mp->m_attr_node_ents =
101                 (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
102                 (uint)sizeof(xfs_da_node_entry_t);
103         mp->m_dir_node_ents =
104                 (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
105                 (uint)sizeof(xfs_da_node_entry_t);
106         mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
107         if (xfs_sb_version_hasasciici(&mp->m_sb))
108                 mp->m_dirnameops = &xfs_ascii_ci_nameops;
109         else
110                 mp->m_dirnameops = &xfs_default_nameops;
111 }
112
113 /*
114  * Return 1 if directory contains only "." and "..".
115  */
116 int
117 xfs_dir_isempty(
118         xfs_inode_t     *dp)
119 {
120         xfs_dir2_sf_hdr_t       *sfp;
121
122         ASSERT(S_ISDIR(dp->i_d.di_mode));
123         if (dp->i_d.di_size == 0)       /* might happen during shutdown. */
124                 return 1;
125         if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
126                 return 0;
127         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
128         return !sfp->count;
129 }
130
131 /*
132  * Validate a given inode number.
133  */
134 int
135 xfs_dir_ino_validate(
136         xfs_mount_t     *mp,
137         xfs_ino_t       ino)
138 {
139         xfs_agblock_t   agblkno;
140         xfs_agino_t     agino;
141         xfs_agnumber_t  agno;
142         int             ino_ok;
143         int             ioff;
144
145         agno = XFS_INO_TO_AGNO(mp, ino);
146         agblkno = XFS_INO_TO_AGBNO(mp, ino);
147         ioff = XFS_INO_TO_OFFSET(mp, ino);
148         agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
149         ino_ok =
150                 agno < mp->m_sb.sb_agcount &&
151                 agblkno < mp->m_sb.sb_agblocks &&
152                 agblkno != 0 &&
153                 ioff < (1 << mp->m_sb.sb_inopblog) &&
154                 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
155         if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
156                         XFS_RANDOM_DIR_INO_VALIDATE))) {
157                 xfs_warn(mp, "Invalid inode number 0x%Lx",
158                                 (unsigned long long) ino);
159                 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
160                 return XFS_ERROR(EFSCORRUPTED);
161         }
162         return 0;
163 }
164
165 /*
166  * Initialize a directory with its "." and ".." entries.
167  */
168 int
169 xfs_dir_init(
170         xfs_trans_t     *tp,
171         xfs_inode_t     *dp,
172         xfs_inode_t     *pdp)
173 {
174         xfs_da_args_t   args;
175         int             error;
176
177         memset((char *)&args, 0, sizeof(args));
178         args.dp = dp;
179         args.trans = tp;
180         ASSERT(S_ISDIR(dp->i_d.di_mode));
181         if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
182                 return error;
183         return xfs_dir2_sf_create(&args, pdp->i_ino);
184 }
185
186 /*
187   Enter a name in a directory.
188  */
189 int
190 xfs_dir_createname(
191         xfs_trans_t             *tp,
192         xfs_inode_t             *dp,
193         struct xfs_name         *name,
194         xfs_ino_t               inum,           /* new entry inode number */
195         xfs_fsblock_t           *first,         /* bmap's firstblock */
196         xfs_bmap_free_t         *flist,         /* bmap's freeblock list */
197         xfs_extlen_t            total)          /* bmap's total block count */
198 {
199         xfs_da_args_t           args;
200         int                     rval;
201         int                     v;              /* type-checking value */
202
203         ASSERT(S_ISDIR(dp->i_d.di_mode));
204         if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
205                 return rval;
206         XFS_STATS_INC(xs_dir_create);
207
208         memset(&args, 0, sizeof(xfs_da_args_t));
209         args.name = name->name;
210         args.namelen = name->len;
211         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
212         args.inumber = inum;
213         args.dp = dp;
214         args.firstblock = first;
215         args.flist = flist;
216         args.total = total;
217         args.whichfork = XFS_DATA_FORK;
218         args.trans = tp;
219         args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
220
221         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
222                 rval = xfs_dir2_sf_addname(&args);
223         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
224                 return rval;
225         else if (v)
226                 rval = xfs_dir2_block_addname(&args);
227         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
228                 return rval;
229         else if (v)
230                 rval = xfs_dir2_leaf_addname(&args);
231         else
232                 rval = xfs_dir2_node_addname(&args);
233         return rval;
234 }
235
236 /*
237  * If doing a CI lookup and case-insensitive match, dup actual name into
238  * args.value. Return EEXIST for success (ie. name found) or an error.
239  */
240 int
241 xfs_dir_cilookup_result(
242         struct xfs_da_args *args,
243         const unsigned char *name,
244         int             len)
245 {
246         if (args->cmpresult == XFS_CMP_DIFFERENT)
247                 return ENOENT;
248         if (args->cmpresult != XFS_CMP_CASE ||
249                                         !(args->op_flags & XFS_DA_OP_CILOOKUP))
250                 return EEXIST;
251
252         args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
253         if (!args->value)
254                 return ENOMEM;
255
256         memcpy(args->value, name, len);
257         args->valuelen = len;
258         return EEXIST;
259 }
260
261 /*
262  * Lookup a name in a directory, give back the inode number.
263  * If ci_name is not NULL, returns the actual name in ci_name if it differs
264  * to name, or ci_name->name is set to NULL for an exact match.
265  */
266
267 int
268 xfs_dir_lookup(
269         xfs_trans_t     *tp,
270         xfs_inode_t     *dp,
271         struct xfs_name *name,
272         xfs_ino_t       *inum,          /* out: inode number */
273         struct xfs_name *ci_name)       /* out: actual name if CI match */
274 {
275         xfs_da_args_t   args;
276         int             rval;
277         int             v;              /* type-checking value */
278
279         ASSERT(S_ISDIR(dp->i_d.di_mode));
280         XFS_STATS_INC(xs_dir_lookup);
281
282         memset(&args, 0, sizeof(xfs_da_args_t));
283         args.name = name->name;
284         args.namelen = name->len;
285         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
286         args.dp = dp;
287         args.whichfork = XFS_DATA_FORK;
288         args.trans = tp;
289         args.op_flags = XFS_DA_OP_OKNOENT;
290         if (ci_name)
291                 args.op_flags |= XFS_DA_OP_CILOOKUP;
292
293         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
294                 rval = xfs_dir2_sf_lookup(&args);
295         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
296                 return rval;
297         else if (v)
298                 rval = xfs_dir2_block_lookup(&args);
299         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
300                 return rval;
301         else if (v)
302                 rval = xfs_dir2_leaf_lookup(&args);
303         else
304                 rval = xfs_dir2_node_lookup(&args);
305         if (rval == EEXIST)
306                 rval = 0;
307         if (!rval) {
308                 *inum = args.inumber;
309                 if (ci_name) {
310                         ci_name->name = args.value;
311                         ci_name->len = args.valuelen;
312                 }
313         }
314         return rval;
315 }
316
317 /*
318  * Remove an entry from a directory.
319  */
320 int
321 xfs_dir_removename(
322         xfs_trans_t     *tp,
323         xfs_inode_t     *dp,
324         struct xfs_name *name,
325         xfs_ino_t       ino,
326         xfs_fsblock_t   *first,         /* bmap's firstblock */
327         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
328         xfs_extlen_t    total)          /* bmap's total block count */
329 {
330         xfs_da_args_t   args;
331         int             rval;
332         int             v;              /* type-checking value */
333
334         ASSERT(S_ISDIR(dp->i_d.di_mode));
335         XFS_STATS_INC(xs_dir_remove);
336
337         memset(&args, 0, sizeof(xfs_da_args_t));
338         args.name = name->name;
339         args.namelen = name->len;
340         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
341         args.inumber = ino;
342         args.dp = dp;
343         args.firstblock = first;
344         args.flist = flist;
345         args.total = total;
346         args.whichfork = XFS_DATA_FORK;
347         args.trans = tp;
348
349         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
350                 rval = xfs_dir2_sf_removename(&args);
351         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
352                 return rval;
353         else if (v)
354                 rval = xfs_dir2_block_removename(&args);
355         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
356                 return rval;
357         else if (v)
358                 rval = xfs_dir2_leaf_removename(&args);
359         else
360                 rval = xfs_dir2_node_removename(&args);
361         return rval;
362 }
363
364 /*
365  * Replace the inode number of a directory entry.
366  */
367 int
368 xfs_dir_replace(
369         xfs_trans_t     *tp,
370         xfs_inode_t     *dp,
371         struct xfs_name *name,          /* name of entry to replace */
372         xfs_ino_t       inum,           /* new inode number */
373         xfs_fsblock_t   *first,         /* bmap's firstblock */
374         xfs_bmap_free_t *flist,         /* bmap's freeblock list */
375         xfs_extlen_t    total)          /* bmap's total block count */
376 {
377         xfs_da_args_t   args;
378         int             rval;
379         int             v;              /* type-checking value */
380
381         ASSERT(S_ISDIR(dp->i_d.di_mode));
382
383         if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
384                 return rval;
385
386         memset(&args, 0, sizeof(xfs_da_args_t));
387         args.name = name->name;
388         args.namelen = name->len;
389         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
390         args.inumber = inum;
391         args.dp = dp;
392         args.firstblock = first;
393         args.flist = flist;
394         args.total = total;
395         args.whichfork = XFS_DATA_FORK;
396         args.trans = tp;
397
398         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
399                 rval = xfs_dir2_sf_replace(&args);
400         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
401                 return rval;
402         else if (v)
403                 rval = xfs_dir2_block_replace(&args);
404         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
405                 return rval;
406         else if (v)
407                 rval = xfs_dir2_leaf_replace(&args);
408         else
409                 rval = xfs_dir2_node_replace(&args);
410         return rval;
411 }
412
413 /*
414  * See if this entry can be added to the directory without allocating space.
415  * First checks that the caller couldn't reserve enough space (resblks = 0).
416  */
417 int
418 xfs_dir_canenter(
419         xfs_trans_t     *tp,
420         xfs_inode_t     *dp,
421         struct xfs_name *name,          /* name of entry to add */
422         uint            resblks)
423 {
424         xfs_da_args_t   args;
425         int             rval;
426         int             v;              /* type-checking value */
427
428         if (resblks)
429                 return 0;
430
431         ASSERT(S_ISDIR(dp->i_d.di_mode));
432
433         memset(&args, 0, sizeof(xfs_da_args_t));
434         args.name = name->name;
435         args.namelen = name->len;
436         args.hashval = dp->i_mount->m_dirnameops->hashname(name);
437         args.dp = dp;
438         args.whichfork = XFS_DATA_FORK;
439         args.trans = tp;
440         args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
441                                                         XFS_DA_OP_OKNOENT;
442
443         if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
444                 rval = xfs_dir2_sf_addname(&args);
445         else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
446                 return rval;
447         else if (v)
448                 rval = xfs_dir2_block_addname(&args);
449         else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
450                 return rval;
451         else if (v)
452                 rval = xfs_dir2_leaf_addname(&args);
453         else
454                 rval = xfs_dir2_node_addname(&args);
455         return rval;
456 }
457
458 /*
459  * Utility routines.
460  */
461
462 /*
463  * Add a block to the directory.
464  *
465  * This routine is for data and free blocks, not leaf/node blocks which are
466  * handled by xfs_da_grow_inode.
467  */
468 int
469 xfs_dir2_grow_inode(
470         struct xfs_da_args      *args,
471         int                     space,  /* v2 dir's space XFS_DIR2_xxx_SPACE */
472         xfs_dir2_db_t           *dbp)   /* out: block number added */
473 {
474         struct xfs_inode        *dp = args->dp;
475         struct xfs_mount        *mp = dp->i_mount;
476         xfs_fileoff_t           bno;    /* directory offset of new block */
477         int                     count;  /* count of filesystem blocks */
478         int                     error;
479
480         trace_xfs_dir2_grow_inode(args, space);
481
482         /*
483          * Set lowest possible block in the space requested.
484          */
485         bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
486         count = mp->m_dirblkfsbs;
487
488         error = xfs_da_grow_inode_int(args, &bno, count);
489         if (error)
490                 return error;
491
492         *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
493
494         /*
495          * Update file's size if this is the data space and it grew.
496          */
497         if (space == XFS_DIR2_DATA_SPACE) {
498                 xfs_fsize_t     size;           /* directory file (data) size */
499
500                 size = XFS_FSB_TO_B(mp, bno + count);
501                 if (size > dp->i_d.di_size) {
502                         dp->i_d.di_size = size;
503                         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
504                 }
505         }
506         return 0;
507 }
508
509 /*
510  * See if the directory is a single-block form directory.
511  */
512 int
513 xfs_dir2_isblock(
514         xfs_trans_t     *tp,
515         xfs_inode_t     *dp,
516         int             *vp)            /* out: 1 is block, 0 is not block */
517 {
518         xfs_fileoff_t   last;           /* last file offset */
519         xfs_mount_t     *mp;
520         int             rval;
521
522         mp = dp->i_mount;
523         if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
524                 return rval;
525         rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
526         ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
527         *vp = rval;
528         return 0;
529 }
530
531 /*
532  * See if the directory is a single-leaf form directory.
533  */
534 int
535 xfs_dir2_isleaf(
536         xfs_trans_t     *tp,
537         xfs_inode_t     *dp,
538         int             *vp)            /* out: 1 is leaf, 0 is not leaf */
539 {
540         xfs_fileoff_t   last;           /* last file offset */
541         xfs_mount_t     *mp;
542         int             rval;
543
544         mp = dp->i_mount;
545         if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
546                 return rval;
547         *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
548         return 0;
549 }
550
551 /*
552  * Remove the given block from the directory.
553  * This routine is used for data and free blocks, leaf/node are done
554  * by xfs_da_shrink_inode.
555  */
556 int
557 xfs_dir2_shrink_inode(
558         xfs_da_args_t   *args,
559         xfs_dir2_db_t   db,
560         struct xfs_buf  *bp)
561 {
562         xfs_fileoff_t   bno;            /* directory file offset */
563         xfs_dablk_t     da;             /* directory file offset */
564         int             done;           /* bunmap is finished */
565         xfs_inode_t     *dp;
566         int             error;
567         xfs_mount_t     *mp;
568         xfs_trans_t     *tp;
569
570         trace_xfs_dir2_shrink_inode(args, db);
571
572         dp = args->dp;
573         mp = dp->i_mount;
574         tp = args->trans;
575         da = xfs_dir2_db_to_da(mp, db);
576         /*
577          * Unmap the fsblock(s).
578          */
579         if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
580                         XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
581                         &done))) {
582                 /*
583                  * ENOSPC actually can happen if we're in a removename with
584                  * no space reservation, and the resulting block removal
585                  * would cause a bmap btree split or conversion from extents
586                  * to btree.  This can only happen for un-fragmented
587                  * directory blocks, since you need to be punching out
588                  * the middle of an extent.
589                  * In this case we need to leave the block in the file,
590                  * and not binval it.
591                  * So the block has to be in a consistent empty state
592                  * and appropriately logged.
593                  * We don't free up the buffer, the caller can tell it
594                  * hasn't happened since it got an error back.
595                  */
596                 return error;
597         }
598         ASSERT(done);
599         /*
600          * Invalidate the buffer from the transaction.
601          */
602         xfs_trans_binval(tp, bp);
603         /*
604          * If it's not a data block, we're done.
605          */
606         if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
607                 return 0;
608         /*
609          * If the block isn't the last one in the directory, we're done.
610          */
611         if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
612                 return 0;
613         bno = da;
614         if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
615                 /*
616                  * This can't really happen unless there's kernel corruption.
617                  */
618                 return error;
619         }
620         if (db == mp->m_dirdatablk)
621                 ASSERT(bno == 0);
622         else
623                 ASSERT(bno > 0);
624         /*
625          * Set the size to the new last block.
626          */
627         dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
628         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
629         return 0;
630 }