~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/afs/dir.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* dir.c: AFS filesystem directory handling
  2  *
  3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4  * Written by David Howells (dhowells@redhat.com)
  5  *
  6  * This program is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU General Public License
  8  * as published by the Free Software Foundation; either version
  9  * 2 of the License, or (at your option) any later version.
 10  */
 11 
 12 #include <linux/kernel.h>
 13 #include <linux/module.h>
 14 #include <linux/init.h>
 15 #include <linux/fs.h>
 16 #include <linux/namei.h>
 17 #include <linux/pagemap.h>
 18 #include <linux/ctype.h>
 19 #include <linux/sched.h>
 20 #include "internal.h"
 21 
 22 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
 23                                  unsigned int flags);
 24 static int afs_dir_open(struct inode *inode, struct file *file);
 25 static int afs_readdir(struct file *file, struct dir_context *ctx);
 26 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
 27 static int afs_d_delete(const struct dentry *dentry);
 28 static void afs_d_release(struct dentry *dentry);
 29 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
 30                                   loff_t fpos, u64 ino, unsigned dtype);
 31 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
 32                       bool excl);
 33 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
 34 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
 35 static int afs_unlink(struct inode *dir, struct dentry *dentry);
 36 static int afs_link(struct dentry *from, struct inode *dir,
 37                     struct dentry *dentry);
 38 static int afs_symlink(struct inode *dir, struct dentry *dentry,
 39                        const char *content);
 40 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
 41                       struct inode *new_dir, struct dentry *new_dentry,
 42                       unsigned int flags);
 43 
 44 const struct file_operations afs_dir_file_operations = {
 45         .open           = afs_dir_open,
 46         .release        = afs_release,
 47         .iterate_shared = afs_readdir,
 48         .lock           = afs_lock,
 49         .llseek         = generic_file_llseek,
 50 };
 51 
 52 const struct inode_operations afs_dir_inode_operations = {
 53         .create         = afs_create,
 54         .lookup         = afs_lookup,
 55         .link           = afs_link,
 56         .unlink         = afs_unlink,
 57         .symlink        = afs_symlink,
 58         .mkdir          = afs_mkdir,
 59         .rmdir          = afs_rmdir,
 60         .rename         = afs_rename,
 61         .permission     = afs_permission,
 62         .getattr        = afs_getattr,
 63         .setattr        = afs_setattr,
 64         .listxattr      = afs_listxattr,
 65 };
 66 
 67 const struct dentry_operations afs_fs_dentry_operations = {
 68         .d_revalidate   = afs_d_revalidate,
 69         .d_delete       = afs_d_delete,
 70         .d_release      = afs_d_release,
 71         .d_automount    = afs_d_automount,
 72 };
 73 
 74 #define AFS_DIR_HASHTBL_SIZE    128
 75 #define AFS_DIR_DIRENT_SIZE     32
 76 #define AFS_DIRENT_PER_BLOCK    64
 77 
 78 union afs_dirent {
 79         struct {
 80                 uint8_t         valid;
 81                 uint8_t         unused[1];
 82                 __be16          hash_next;
 83                 __be32          vnode;
 84                 __be32          unique;
 85                 uint8_t         name[16];
 86                 uint8_t         overflow[4];    /* if any char of the name (inc
 87                                                  * NUL) reaches here, consume
 88                                                  * the next dirent too */
 89         } u;
 90         uint8_t extended_name[32];
 91 };
 92 
 93 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
 94 struct afs_dir_pagehdr {
 95         __be16          npages;
 96         __be16          magic;
 97 #define AFS_DIR_MAGIC htons(1234)
 98         uint8_t         nentries;
 99         uint8_t         bitmap[8];
100         uint8_t         pad[19];
101 };
102 
103 /* directory block layout */
104 union afs_dir_block {
105 
106         struct afs_dir_pagehdr pagehdr;
107 
108         struct {
109                 struct afs_dir_pagehdr  pagehdr;
110                 uint8_t                 alloc_ctrs[128];
111                 /* dir hash table */
112                 uint16_t                hashtable[AFS_DIR_HASHTBL_SIZE];
113         } hdr;
114 
115         union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
116 };
117 
118 /* layout on a linux VM page */
119 struct afs_dir_page {
120         union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
121 };
122 
123 struct afs_lookup_cookie {
124         struct dir_context ctx;
125         struct afs_fid  fid;
126         struct qstr name;
127         int             found;
128 };
129 
130 /*
131  * check that a directory page is valid
132  */
133 bool afs_dir_check_page(struct inode *dir, struct page *page)
134 {
135         struct afs_dir_page *dbuf;
136         struct afs_vnode *vnode = AFS_FS_I(dir);
137         loff_t latter, i_size, off;
138         int tmp, qty;
139 
140 #if 0
141         /* check the page count */
142         qty = desc.size / sizeof(dbuf->blocks[0]);
143         if (qty == 0)
144                 goto error;
145 
146         if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
147                 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
148                        __func__, dir->i_ino, qty,
149                        ntohs(dbuf->blocks[0].pagehdr.npages));
150                 goto error;
151         }
152 #endif
153 
154         /* Determine how many magic numbers there should be in this page, but
155          * we must take care because the directory may change size under us.
156          */
157         off = page_offset(page);
158         i_size = i_size_read(dir);
159         if (i_size <= off)
160                 goto checked;
161 
162         latter = i_size - off;
163         if (latter >= PAGE_SIZE)
164                 qty = PAGE_SIZE;
165         else
166                 qty = latter;
167         qty /= sizeof(union afs_dir_block);
168 
169         /* check them */
170         dbuf = page_address(page);
171         for (tmp = 0; tmp < qty; tmp++) {
172                 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
173                         printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
174                                __func__, dir->i_ino, tmp, qty,
175                                ntohs(dbuf->blocks[tmp].pagehdr.magic));
176                         trace_afs_dir_check_failed(vnode, off, i_size);
177                         goto error;
178                 }
179         }
180 
181 checked:
182         SetPageChecked(page);
183         return true;
184 
185 error:
186         SetPageError(page);
187         return false;
188 }
189 
190 /*
191  * discard a page cached in the pagecache
192  */
193 static inline void afs_dir_put_page(struct page *page)
194 {
195         kunmap(page);
196         unlock_page(page);
197         put_page(page);
198 }
199 
200 /*
201  * get a page into the pagecache
202  */
203 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
204                                      struct key *key)
205 {
206         struct page *page;
207         _enter("{%lu},%lu", dir->i_ino, index);
208 
209         page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
210         if (!IS_ERR(page)) {
211                 lock_page(page);
212                 kmap(page);
213                 if (unlikely(!PageChecked(page))) {
214                         if (PageError(page))
215                                 goto fail;
216                 }
217         }
218         return page;
219 
220 fail:
221         afs_dir_put_page(page);
222         _leave(" = -EIO");
223         return ERR_PTR(-EIO);
224 }
225 
226 /*
227  * open an AFS directory file
228  */
229 static int afs_dir_open(struct inode *inode, struct file *file)
230 {
231         _enter("{%lu}", inode->i_ino);
232 
233         BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
234         BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
235 
236         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
237                 return -ENOENT;
238 
239         return afs_open(inode, file);
240 }
241 
242 /*
243  * deal with one block in an AFS directory
244  */
245 static int afs_dir_iterate_block(struct dir_context *ctx,
246                                  union afs_dir_block *block,
247                                  unsigned blkoff)
248 {
249         union afs_dirent *dire;
250         unsigned offset, next, curr;
251         size_t nlen;
252         int tmp;
253 
254         _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
255 
256         curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
257 
258         /* walk through the block, an entry at a time */
259         for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
260              offset < AFS_DIRENT_PER_BLOCK;
261              offset = next
262              ) {
263                 next = offset + 1;
264 
265                 /* skip entries marked unused in the bitmap */
266                 if (!(block->pagehdr.bitmap[offset / 8] &
267                       (1 << (offset % 8)))) {
268                         _debug("ENT[%zu.%u]: unused",
269                                blkoff / sizeof(union afs_dir_block), offset);
270                         if (offset >= curr)
271                                 ctx->pos = blkoff +
272                                         next * sizeof(union afs_dirent);
273                         continue;
274                 }
275 
276                 /* got a valid entry */
277                 dire = &block->dirents[offset];
278                 nlen = strnlen(dire->u.name,
279                                sizeof(*block) -
280                                offset * sizeof(union afs_dirent));
281 
282                 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
283                        blkoff / sizeof(union afs_dir_block), offset,
284                        (offset < curr ? "skip" : "fill"),
285                        nlen, dire->u.name);
286 
287                 /* work out where the next possible entry is */
288                 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
289                         if (next >= AFS_DIRENT_PER_BLOCK) {
290                                 _debug("ENT[%zu.%u]:"
291                                        " %u travelled beyond end dir block"
292                                        " (len %u/%zu)",
293                                        blkoff / sizeof(union afs_dir_block),
294                                        offset, next, tmp, nlen);
295                                 return -EIO;
296                         }
297                         if (!(block->pagehdr.bitmap[next / 8] &
298                               (1 << (next % 8)))) {
299                                 _debug("ENT[%zu.%u]:"
300                                        " %u unmarked extension (len %u/%zu)",
301                                        blkoff / sizeof(union afs_dir_block),
302                                        offset, next, tmp, nlen);
303                                 return -EIO;
304                         }
305 
306                         _debug("ENT[%zu.%u]: ext %u/%zu",
307                                blkoff / sizeof(union afs_dir_block),
308                                next, tmp, nlen);
309                         next++;
310                 }
311 
312                 /* skip if starts before the current position */
313                 if (offset < curr)
314                         continue;
315 
316                 /* found the next entry */
317                 if (!dir_emit(ctx, dire->u.name, nlen,
318                               ntohl(dire->u.vnode),
319                               ctx->actor == afs_lookup_filldir ?
320                               ntohl(dire->u.unique) : DT_UNKNOWN)) {
321                         _leave(" = 0 [full]");
322                         return 0;
323                 }
324 
325                 ctx->pos = blkoff + next * sizeof(union afs_dirent);
326         }
327 
328         _leave(" = 1 [more]");
329         return 1;
330 }
331 
332 /*
333  * iterate through the data blob that lists the contents of an AFS directory
334  */
335 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
336                            struct key *key)
337 {
338         union afs_dir_block *dblock;
339         struct afs_dir_page *dbuf;
340         struct page *page;
341         unsigned blkoff, limit;
342         int ret;
343 
344         _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
345 
346         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
347                 _leave(" = -ESTALE");
348                 return -ESTALE;
349         }
350 
351         /* round the file position up to the next entry boundary */
352         ctx->pos += sizeof(union afs_dirent) - 1;
353         ctx->pos &= ~(sizeof(union afs_dirent) - 1);
354 
355         /* walk through the blocks in sequence */
356         ret = 0;
357         while (ctx->pos < dir->i_size) {
358                 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
359 
360                 /* fetch the appropriate page from the directory */
361                 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
362                 if (IS_ERR(page)) {
363                         ret = PTR_ERR(page);
364                         break;
365                 }
366 
367                 limit = blkoff & ~(PAGE_SIZE - 1);
368 
369                 dbuf = page_address(page);
370 
371                 /* deal with the individual blocks stashed on this page */
372                 do {
373                         dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
374                                                sizeof(union afs_dir_block)];
375                         ret = afs_dir_iterate_block(ctx, dblock, blkoff);
376                         if (ret != 1) {
377                                 afs_dir_put_page(page);
378                                 goto out;
379                         }
380 
381                         blkoff += sizeof(union afs_dir_block);
382 
383                 } while (ctx->pos < dir->i_size && blkoff < limit);
384 
385                 afs_dir_put_page(page);
386                 ret = 0;
387         }
388 
389 out:
390         _leave(" = %d", ret);
391         return ret;
392 }
393 
394 /*
395  * read an AFS directory
396  */
397 static int afs_readdir(struct file *file, struct dir_context *ctx)
398 {
399         return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
400 }
401 
402 /*
403  * search the directory for a name
404  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
405  *   uniquifier through dtype
406  */
407 static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
408                               int nlen, loff_t fpos, u64 ino, unsigned dtype)
409 {
410         struct afs_lookup_cookie *cookie =
411                 container_of(ctx, struct afs_lookup_cookie, ctx);
412 
413         _enter("{%s,%u},%s,%u,,%llu,%u",
414                cookie->name.name, cookie->name.len, name, nlen,
415                (unsigned long long) ino, dtype);
416 
417         /* insanity checks first */
418         BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
419         BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
420 
421         if (cookie->name.len != nlen ||
422             memcmp(cookie->name.name, name, nlen) != 0) {
423                 _leave(" = 0 [no]");
424                 return 0;
425         }
426 
427         cookie->fid.vnode = ino;
428         cookie->fid.unique = dtype;
429         cookie->found = 1;
430 
431         _leave(" = -1 [found]");
432         return -1;
433 }
434 
435 /*
436  * do a lookup in a directory
437  * - just returns the FID the dentry name maps to if found
438  */
439 static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
440                          struct afs_fid *fid, struct key *key)
441 {
442         struct afs_super_info *as = dir->i_sb->s_fs_info;
443         struct afs_lookup_cookie cookie = {
444                 .ctx.actor = afs_lookup_filldir,
445                 .name = dentry->d_name,
446                 .fid.vid = as->volume->vid
447         };
448         int ret;
449 
450         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
451 
452         /* search the directory */
453         ret = afs_dir_iterate(dir, &cookie.ctx, key);
454         if (ret < 0) {
455                 _leave(" = %d [iter]", ret);
456                 return ret;
457         }
458 
459         ret = -ENOENT;
460         if (!cookie.found) {
461                 _leave(" = -ENOENT [not found]");
462                 return -ENOENT;
463         }
464 
465         *fid = cookie.fid;
466         _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
467         return 0;
468 }
469 
470 /*
471  * Try to auto mount the mountpoint with pseudo directory, if the autocell
472  * operation is setted.
473  */
474 static struct inode *afs_try_auto_mntpt(
475         int ret, struct dentry *dentry, struct inode *dir, struct key *key,
476         struct afs_fid *fid)
477 {
478         const char *devname = dentry->d_name.name;
479         struct afs_vnode *vnode = AFS_FS_I(dir);
480         struct inode *inode;
481 
482         _enter("%d, %p{%pd}, {%x:%u}, %p",
483                ret, dentry, dentry, vnode->fid.vid, vnode->fid.vnode, key);
484 
485         if (ret != -ENOENT ||
486             !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
487                 goto out;
488 
489         inode = afs_iget_autocell(dir, devname, strlen(devname), key);
490         if (IS_ERR(inode)) {
491                 ret = PTR_ERR(inode);
492                 goto out;
493         }
494 
495         *fid = AFS_FS_I(inode)->fid;
496         _leave("= %p", inode);
497         return inode;
498 
499 out:
500         _leave("= %d", ret);
501         return ERR_PTR(ret);
502 }
503 
504 /*
505  * look up an entry in a directory
506  */
507 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
508                                  unsigned int flags)
509 {
510         struct afs_vnode *vnode;
511         struct afs_fid fid;
512         struct inode *inode;
513         struct key *key;
514         int ret;
515 
516         vnode = AFS_FS_I(dir);
517 
518         _enter("{%x:%u},%p{%pd},",
519                vnode->fid.vid, vnode->fid.vnode, dentry, dentry);
520 
521         ASSERTCMP(d_inode(dentry), ==, NULL);
522 
523         if (dentry->d_name.len >= AFSNAMEMAX) {
524                 _leave(" = -ENAMETOOLONG");
525                 return ERR_PTR(-ENAMETOOLONG);
526         }
527 
528         if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
529                 _leave(" = -ESTALE");
530                 return ERR_PTR(-ESTALE);
531         }
532 
533         key = afs_request_key(vnode->volume->cell);
534         if (IS_ERR(key)) {
535                 _leave(" = %ld [key]", PTR_ERR(key));
536                 return ERR_CAST(key);
537         }
538 
539         ret = afs_validate(vnode, key);
540         if (ret < 0) {
541                 key_put(key);
542                 _leave(" = %d [val]", ret);
543                 return ERR_PTR(ret);
544         }
545 
546         ret = afs_do_lookup(dir, dentry, &fid, key);
547         if (ret < 0) {
548                 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
549                 if (!IS_ERR(inode)) {
550                         key_put(key);
551                         goto success;
552                 }
553 
554                 ret = PTR_ERR(inode);
555                 key_put(key);
556                 if (ret == -ENOENT) {
557                         d_add(dentry, NULL);
558                         _leave(" = NULL [negative]");
559                         return NULL;
560                 }
561                 _leave(" = %d [do]", ret);
562                 return ERR_PTR(ret);
563         }
564         dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
565 
566         /* instantiate the dentry */
567         inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL, NULL);
568         key_put(key);
569         if (IS_ERR(inode)) {
570                 _leave(" = %ld", PTR_ERR(inode));
571                 return ERR_CAST(inode);
572         }
573 
574 success:
575         d_add(dentry, inode);
576         _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
577                fid.vnode,
578                fid.unique,
579                d_inode(dentry)->i_ino,
580                d_inode(dentry)->i_generation);
581 
582         return NULL;
583 }
584 
585 /*
586  * check that a dentry lookup hit has found a valid entry
587  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
588  *   inode
589  */
590 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
591 {
592         struct afs_vnode *vnode, *dir;
593         struct afs_fid uninitialized_var(fid);
594         struct dentry *parent;
595         struct inode *inode;
596         struct key *key;
597         void *dir_version;
598         int ret;
599 
600         if (flags & LOOKUP_RCU)
601                 return -ECHILD;
602 
603         if (d_really_is_positive(dentry)) {
604                 vnode = AFS_FS_I(d_inode(dentry));
605                 _enter("{v={%x:%u} n=%pd fl=%lx},",
606                        vnode->fid.vid, vnode->fid.vnode, dentry,
607                        vnode->flags);
608         } else {
609                 _enter("{neg n=%pd}", dentry);
610         }
611 
612         key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
613         if (IS_ERR(key))
614                 key = NULL;
615 
616         if (d_really_is_positive(dentry)) {
617                 inode = d_inode(dentry);
618                 if (inode) {
619                         vnode = AFS_FS_I(inode);
620                         afs_validate(vnode, key);
621                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
622                                 goto out_bad;
623                 }
624         }
625 
626         /* lock down the parent dentry so we can peer at it */
627         parent = dget_parent(dentry);
628         dir = AFS_FS_I(d_inode(parent));
629 
630         /* validate the parent directory */
631         afs_validate(dir, key);
632 
633         if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
634                 _debug("%pd: parent dir deleted", dentry);
635                 goto out_bad_parent;
636         }
637 
638         dir_version = (void *) (unsigned long) dir->status.data_version;
639         if (dentry->d_fsdata == dir_version)
640                 goto out_valid; /* the dir contents are unchanged */
641 
642         _debug("dir modified");
643 
644         /* search the directory for this vnode */
645         ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
646         switch (ret) {
647         case 0:
648                 /* the filename maps to something */
649                 if (d_really_is_negative(dentry))
650                         goto out_bad_parent;
651                 inode = d_inode(dentry);
652                 if (is_bad_inode(inode)) {
653                         printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
654                                dentry);
655                         goto out_bad_parent;
656                 }
657 
658                 vnode = AFS_FS_I(inode);
659 
660                 /* if the vnode ID has changed, then the dirent points to a
661                  * different file */
662                 if (fid.vnode != vnode->fid.vnode) {
663                         _debug("%pd: dirent changed [%u != %u]",
664                                dentry, fid.vnode,
665                                vnode->fid.vnode);
666                         goto not_found;
667                 }
668 
669                 /* if the vnode ID uniqifier has changed, then the file has
670                  * been deleted and replaced, and the original vnode ID has
671                  * been reused */
672                 if (fid.unique != vnode->fid.unique) {
673                         _debug("%pd: file deleted (uq %u -> %u I:%u)",
674                                dentry, fid.unique,
675                                vnode->fid.unique,
676                                vnode->vfs_inode.i_generation);
677                         write_seqlock(&vnode->cb_lock);
678                         set_bit(AFS_VNODE_DELETED, &vnode->flags);
679                         write_sequnlock(&vnode->cb_lock);
680                         goto not_found;
681                 }
682                 goto out_valid;
683 
684         case -ENOENT:
685                 /* the filename is unknown */
686                 _debug("%pd: dirent not found", dentry);
687                 if (d_really_is_positive(dentry))
688                         goto not_found;
689                 goto out_valid;
690 
691         default:
692                 _debug("failed to iterate dir %pd: %d",
693                        parent, ret);
694                 goto out_bad_parent;
695         }
696 
697 out_valid:
698         dentry->d_fsdata = dir_version;
699         dput(parent);
700         key_put(key);
701         _leave(" = 1 [valid]");
702         return 1;
703 
704         /* the dirent, if it exists, now points to a different vnode */
705 not_found:
706         spin_lock(&dentry->d_lock);
707         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
708         spin_unlock(&dentry->d_lock);
709 
710 out_bad_parent:
711         _debug("dropping dentry %pd2", dentry);
712         dput(parent);
713 out_bad:
714         key_put(key);
715 
716         _leave(" = 0 [bad]");
717         return 0;
718 }
719 
720 /*
721  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
722  * sleep)
723  * - called from dput() when d_count is going to 0.
724  * - return 1 to request dentry be unhashed, 0 otherwise
725  */
726 static int afs_d_delete(const struct dentry *dentry)
727 {
728         _enter("%pd", dentry);
729 
730         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
731                 goto zap;
732 
733         if (d_really_is_positive(dentry) &&
734             (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
735              test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
736                 goto zap;
737 
738         _leave(" = 0 [keep]");
739         return 0;
740 
741 zap:
742         _leave(" = 1 [zap]");
743         return 1;
744 }
745 
746 /*
747  * handle dentry release
748  */
749 static void afs_d_release(struct dentry *dentry)
750 {
751         _enter("%pd", dentry);
752 }
753 
754 /*
755  * Create a new inode for create/mkdir/symlink
756  */
757 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
758                                 struct dentry *new_dentry,
759                                 struct afs_fid *newfid,
760                                 struct afs_file_status *newstatus,
761                                 struct afs_callback *newcb)
762 {
763         struct inode *inode;
764 
765         if (fc->ac.error < 0)
766                 return;
767 
768         d_drop(new_dentry);
769 
770         inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
771                          newfid, newstatus, newcb, fc->cbi);
772         if (IS_ERR(inode)) {
773                 /* ENOMEM or EINTR at a really inconvenient time - just abandon
774                  * the new directory on the server.
775                  */
776                 fc->ac.error = PTR_ERR(inode);
777                 return;
778         }
779 
780         d_add(new_dentry, inode);
781 }
782 
783 /*
784  * create a directory on an AFS filesystem
785  */
786 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
787 {
788         struct afs_file_status newstatus;
789         struct afs_fs_cursor fc;
790         struct afs_callback newcb;
791         struct afs_vnode *dvnode = AFS_FS_I(dir);
792         struct afs_fid newfid;
793         struct key *key;
794         int ret;
795 
796         mode |= S_IFDIR;
797 
798         _enter("{%x:%u},{%pd},%ho",
799                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
800 
801         key = afs_request_key(dvnode->volume->cell);
802         if (IS_ERR(key)) {
803                 ret = PTR_ERR(key);
804                 goto error;
805         }
806 
807         ret = -ERESTARTSYS;
808         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
809                 while (afs_select_fileserver(&fc)) {
810                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
811                         afs_fs_create(&fc, dentry->d_name.name, mode,
812                                       &newfid, &newstatus, &newcb);
813                 }
814 
815                 afs_check_for_remote_deletion(&fc, fc.vnode);
816                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
817                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
818                 ret = afs_end_vnode_operation(&fc);
819                 if (ret < 0)
820                         goto error_key;
821         } else {
822                 goto error_key;
823         }
824 
825         key_put(key);
826         _leave(" = 0");
827         return 0;
828 
829 error_key:
830         key_put(key);
831 error:
832         d_drop(dentry);
833         _leave(" = %d", ret);
834         return ret;
835 }
836 
837 /*
838  * Remove a subdir from a directory.
839  */
840 static void afs_dir_remove_subdir(struct dentry *dentry)
841 {
842         if (d_really_is_positive(dentry)) {
843                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
844 
845                 clear_nlink(&vnode->vfs_inode);
846                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
847                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
848         }
849 }
850 
851 /*
852  * remove a directory from an AFS filesystem
853  */
854 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
855 {
856         struct afs_fs_cursor fc;
857         struct afs_vnode *dvnode = AFS_FS_I(dir);
858         struct key *key;
859         int ret;
860 
861         _enter("{%x:%u},{%pd}",
862                dvnode->fid.vid, dvnode->fid.vnode, dentry);
863 
864         key = afs_request_key(dvnode->volume->cell);
865         if (IS_ERR(key)) {
866                 ret = PTR_ERR(key);
867                 goto error;
868         }
869 
870         ret = -ERESTARTSYS;
871         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
872                 while (afs_select_fileserver(&fc)) {
873                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
874                         afs_fs_remove(&fc, dentry->d_name.name, true);
875                 }
876 
877                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
878                 ret = afs_end_vnode_operation(&fc);
879                 if (ret == 0)
880                         afs_dir_remove_subdir(dentry);
881         }
882 
883         key_put(key);
884 error:
885         return ret;
886 }
887 
888 /*
889  * Remove a link to a file or symlink from a directory.
890  *
891  * If the file was not deleted due to excess hard links, the fileserver will
892  * break the callback promise on the file - if it had one - before it returns
893  * to us, and if it was deleted, it won't
894  *
895  * However, if we didn't have a callback promise outstanding, or it was
896  * outstanding on a different server, then it won't break it either...
897  */
898 static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
899                                unsigned long d_version_before,
900                                unsigned long d_version_after)
901 {
902         bool dir_valid;
903         int ret = 0;
904 
905         /* There were no intervening changes on the server if the version
906          * number we got back was incremented by exactly 1.
907          */
908         dir_valid = (d_version_after == d_version_before + 1);
909 
910         if (d_really_is_positive(dentry)) {
911                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
912 
913                 if (dir_valid) {
914                         drop_nlink(&vnode->vfs_inode);
915                         if (vnode->vfs_inode.i_nlink == 0) {
916                                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
917                                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
918                         }
919                         ret = 0;
920                 } else {
921                         clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
922 
923                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
924                                 kdebug("AFS_VNODE_DELETED");
925 
926                         ret = afs_validate(vnode, key);
927                         if (ret == -ESTALE)
928                                 ret = 0;
929                 }
930                 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
931         }
932 
933         return ret;
934 }
935 
936 /*
937  * Remove a file or symlink from an AFS filesystem.
938  */
939 static int afs_unlink(struct inode *dir, struct dentry *dentry)
940 {
941         struct afs_fs_cursor fc;
942         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
943         struct key *key;
944         unsigned long d_version = (unsigned long)dentry->d_fsdata;
945         int ret;
946 
947         _enter("{%x:%u},{%pd}",
948                dvnode->fid.vid, dvnode->fid.vnode, dentry);
949 
950         if (dentry->d_name.len >= AFSNAMEMAX)
951                 return -ENAMETOOLONG;
952 
953         key = afs_request_key(dvnode->volume->cell);
954         if (IS_ERR(key)) {
955                 ret = PTR_ERR(key);
956                 goto error;
957         }
958 
959         /* Try to make sure we have a callback promise on the victim. */
960         if (d_really_is_positive(dentry)) {
961                 vnode = AFS_FS_I(d_inode(dentry));
962                 ret = afs_validate(vnode, key);
963                 if (ret < 0)
964                         goto error_key;
965         }
966 
967         ret = -ERESTARTSYS;
968         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
969                 while (afs_select_fileserver(&fc)) {
970                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
971                         afs_fs_remove(&fc, dentry->d_name.name, false);
972                 }
973 
974                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
975                 ret = afs_end_vnode_operation(&fc);
976                 if (ret == 0)
977                         ret = afs_dir_remove_link(
978                                 dentry, key, d_version,
979                                 (unsigned long)dvnode->status.data_version);
980         }
981 
982 error_key:
983         key_put(key);
984 error:
985         _leave(" = %d", ret);
986         return ret;
987 }
988 
989 /*
990  * create a regular file on an AFS filesystem
991  */
992 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
993                       bool excl)
994 {
995         struct afs_fs_cursor fc;
996         struct afs_file_status newstatus;
997         struct afs_callback newcb;
998         struct afs_vnode *dvnode = AFS_FS_I(dir);
999         struct afs_fid newfid;
1000         struct key *key;
1001         int ret;
1002 
1003         mode |= S_IFREG;
1004 
1005         _enter("{%x:%u},{%pd},%ho,",
1006                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1007 
1008         ret = -ENAMETOOLONG;
1009         if (dentry->d_name.len >= AFSNAMEMAX)
1010                 goto error;
1011 
1012         key = afs_request_key(dvnode->volume->cell);
1013         if (IS_ERR(key)) {
1014                 ret = PTR_ERR(key);
1015                 goto error;
1016         }
1017 
1018         ret = -ERESTARTSYS;
1019         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1020                 while (afs_select_fileserver(&fc)) {
1021                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1022                         afs_fs_create(&fc, dentry->d_name.name, mode,
1023                                       &newfid, &newstatus, &newcb);
1024                 }
1025 
1026                 afs_check_for_remote_deletion(&fc, fc.vnode);
1027                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1028                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1029                 ret = afs_end_vnode_operation(&fc);
1030                 if (ret < 0)
1031                         goto error_key;
1032         } else {
1033                 goto error_key;
1034         }
1035 
1036         key_put(key);
1037         _leave(" = 0");
1038         return 0;
1039 
1040 error_key:
1041         key_put(key);
1042 error:
1043         d_drop(dentry);
1044         _leave(" = %d", ret);
1045         return ret;
1046 }
1047 
1048 /*
1049  * create a hard link between files in an AFS filesystem
1050  */
1051 static int afs_link(struct dentry *from, struct inode *dir,
1052                     struct dentry *dentry)
1053 {
1054         struct afs_fs_cursor fc;
1055         struct afs_vnode *dvnode, *vnode;
1056         struct key *key;
1057         int ret;
1058 
1059         vnode = AFS_FS_I(d_inode(from));
1060         dvnode = AFS_FS_I(dir);
1061 
1062         _enter("{%x:%u},{%x:%u},{%pd}",
1063                vnode->fid.vid, vnode->fid.vnode,
1064                dvnode->fid.vid, dvnode->fid.vnode,
1065                dentry);
1066 
1067         ret = -ENAMETOOLONG;
1068         if (dentry->d_name.len >= AFSNAMEMAX)
1069                 goto error;
1070 
1071         key = afs_request_key(dvnode->volume->cell);
1072         if (IS_ERR(key)) {
1073                 ret = PTR_ERR(key);
1074                 goto error;
1075         }
1076 
1077         ret = -ERESTARTSYS;
1078         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1079                 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1080                         afs_end_vnode_operation(&fc);
1081                         goto error_key;
1082                 }
1083 
1084                 while (afs_select_fileserver(&fc)) {
1085                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1086                         fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break;
1087                         afs_fs_link(&fc, vnode, dentry->d_name.name);
1088                 }
1089 
1090                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1091                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1092                 ihold(&vnode->vfs_inode);
1093                 d_instantiate(dentry, &vnode->vfs_inode);
1094 
1095                 mutex_unlock(&vnode->io_lock);
1096                 ret = afs_end_vnode_operation(&fc);
1097                 if (ret < 0)
1098                         goto error_key;
1099         } else {
1100                 goto error_key;
1101         }
1102 
1103         key_put(key);
1104         _leave(" = 0");
1105         return 0;
1106 
1107 error_key:
1108         key_put(key);
1109 error:
1110         d_drop(dentry);
1111         _leave(" = %d", ret);
1112         return ret;
1113 }
1114 
1115 /*
1116  * create a symlink in an AFS filesystem
1117  */
1118 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1119                        const char *content)
1120 {
1121         struct afs_fs_cursor fc;
1122         struct afs_file_status newstatus;
1123         struct afs_vnode *dvnode = AFS_FS_I(dir);
1124         struct afs_fid newfid;
1125         struct key *key;
1126         int ret;
1127 
1128         _enter("{%x:%u},{%pd},%s",
1129                dvnode->fid.vid, dvnode->fid.vnode, dentry,
1130                content);
1131 
1132         ret = -ENAMETOOLONG;
1133         if (dentry->d_name.len >= AFSNAMEMAX)
1134                 goto error;
1135 
1136         ret = -EINVAL;
1137         if (strlen(content) >= AFSPATHMAX)
1138                 goto error;
1139 
1140         key = afs_request_key(dvnode->volume->cell);
1141         if (IS_ERR(key)) {
1142                 ret = PTR_ERR(key);
1143                 goto error;
1144         }
1145 
1146         ret = -ERESTARTSYS;
1147         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1148                 while (afs_select_fileserver(&fc)) {
1149                         fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1150                         afs_fs_symlink(&fc, dentry->d_name.name, content,
1151                                        &newfid, &newstatus);
1152                 }
1153 
1154                 afs_check_for_remote_deletion(&fc, fc.vnode);
1155                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1156                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1157                 ret = afs_end_vnode_operation(&fc);
1158                 if (ret < 0)
1159                         goto error_key;
1160         } else {
1161                 goto error_key;
1162         }
1163 
1164         key_put(key);
1165         _leave(" = 0");
1166         return 0;
1167 
1168 error_key:
1169         key_put(key);
1170 error:
1171         d_drop(dentry);
1172         _leave(" = %d", ret);
1173         return ret;
1174 }
1175 
1176 /*
1177  * rename a file in an AFS filesystem and/or move it between directories
1178  */
1179 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1180                       struct inode *new_dir, struct dentry *new_dentry,
1181                       unsigned int flags)
1182 {
1183         struct afs_fs_cursor fc;
1184         struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1185         struct key *key;
1186         int ret;
1187 
1188         if (flags)
1189                 return -EINVAL;
1190 
1191         vnode = AFS_FS_I(d_inode(old_dentry));
1192         orig_dvnode = AFS_FS_I(old_dir);
1193         new_dvnode = AFS_FS_I(new_dir);
1194 
1195         _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
1196                orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1197                vnode->fid.vid, vnode->fid.vnode,
1198                new_dvnode->fid.vid, new_dvnode->fid.vnode,
1199                new_dentry);
1200 
1201         key = afs_request_key(orig_dvnode->volume->cell);
1202         if (IS_ERR(key)) {
1203                 ret = PTR_ERR(key);
1204                 goto error;
1205         }
1206 
1207         ret = -ERESTARTSYS;
1208         if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1209                 if (orig_dvnode != new_dvnode) {
1210                         if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1211                                 afs_end_vnode_operation(&fc);
1212                                 goto error_key;
1213                         }
1214                 }
1215                 while (afs_select_fileserver(&fc)) {
1216                         fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break;
1217                         fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break;
1218                         afs_fs_rename(&fc, old_dentry->d_name.name,
1219                                       new_dvnode, new_dentry->d_name.name);
1220                 }
1221 
1222                 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1223                 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1224                 if (orig_dvnode != new_dvnode)
1225                         mutex_unlock(&new_dvnode->io_lock);
1226                 ret = afs_end_vnode_operation(&fc);
1227                 if (ret < 0)
1228                         goto error_key;
1229         }
1230 
1231 error_key:
1232         key_put(key);
1233 error:
1234         _leave(" = %d", ret);
1235         return ret;
1236 }
1237 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp