Merge branch 'for-5.4/apple' into for-linus
[sfrench/cifs-2.6.git] / fs / afs / dir.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* dir.c: AFS filesystem directory handling
3  *
4  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/fs.h>
10 #include <linux/namei.h>
11 #include <linux/pagemap.h>
12 #include <linux/swap.h>
13 #include <linux/ctype.h>
14 #include <linux/sched.h>
15 #include <linux/task_io_accounting_ops.h>
16 #include "internal.h"
17 #include "afs_fs.h"
18 #include "xdr_fs.h"
19
20 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
21                                  unsigned int flags);
22 static int afs_dir_open(struct inode *inode, struct file *file);
23 static int afs_readdir(struct file *file, struct dir_context *ctx);
24 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
25 static int afs_d_delete(const struct dentry *dentry);
26 static void afs_d_iput(struct dentry *dentry, struct inode *inode);
27 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
28                                   loff_t fpos, u64 ino, unsigned dtype);
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 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
44 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
45                                    unsigned int length);
46
47 static int afs_dir_set_page_dirty(struct page *page)
48 {
49         BUG(); /* This should never happen. */
50 }
51
52 const struct file_operations afs_dir_file_operations = {
53         .open           = afs_dir_open,
54         .release        = afs_release,
55         .iterate_shared = afs_readdir,
56         .lock           = afs_lock,
57         .llseek         = generic_file_llseek,
58 };
59
60 const struct inode_operations afs_dir_inode_operations = {
61         .create         = afs_create,
62         .lookup         = afs_lookup,
63         .link           = afs_link,
64         .unlink         = afs_unlink,
65         .symlink        = afs_symlink,
66         .mkdir          = afs_mkdir,
67         .rmdir          = afs_rmdir,
68         .rename         = afs_rename,
69         .permission     = afs_permission,
70         .getattr        = afs_getattr,
71         .setattr        = afs_setattr,
72         .listxattr      = afs_listxattr,
73 };
74
75 const struct address_space_operations afs_dir_aops = {
76         .set_page_dirty = afs_dir_set_page_dirty,
77         .releasepage    = afs_dir_releasepage,
78         .invalidatepage = afs_dir_invalidatepage,
79 };
80
81 const struct dentry_operations afs_fs_dentry_operations = {
82         .d_revalidate   = afs_d_revalidate,
83         .d_delete       = afs_d_delete,
84         .d_release      = afs_d_release,
85         .d_automount    = afs_d_automount,
86         .d_iput         = afs_d_iput,
87 };
88
89 struct afs_lookup_one_cookie {
90         struct dir_context      ctx;
91         struct qstr             name;
92         bool                    found;
93         struct afs_fid          fid;
94 };
95
96 struct afs_lookup_cookie {
97         struct dir_context      ctx;
98         struct qstr             name;
99         bool                    found;
100         bool                    one_only;
101         unsigned short          nr_fids;
102         struct inode            **inodes;
103         struct afs_status_cb    *statuses;
104         struct afs_fid          fids[50];
105 };
106
107 /*
108  * check that a directory page is valid
109  */
110 static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
111                                loff_t i_size)
112 {
113         struct afs_xdr_dir_page *dbuf;
114         loff_t latter, off;
115         int tmp, qty;
116
117         /* Determine how many magic numbers there should be in this page, but
118          * we must take care because the directory may change size under us.
119          */
120         off = page_offset(page);
121         if (i_size <= off)
122                 goto checked;
123
124         latter = i_size - off;
125         if (latter >= PAGE_SIZE)
126                 qty = PAGE_SIZE;
127         else
128                 qty = latter;
129         qty /= sizeof(union afs_xdr_dir_block);
130
131         /* check them */
132         dbuf = kmap(page);
133         for (tmp = 0; tmp < qty; tmp++) {
134                 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
135                         printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
136                                __func__, dvnode->vfs_inode.i_ino, tmp, qty,
137                                ntohs(dbuf->blocks[tmp].hdr.magic));
138                         trace_afs_dir_check_failed(dvnode, off, i_size);
139                         kunmap(page);
140                         trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
141                         goto error;
142                 }
143
144                 /* Make sure each block is NUL terminated so we can reasonably
145                  * use string functions on it.  The filenames in the page
146                  * *should* be NUL-terminated anyway.
147                  */
148                 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
149         }
150
151         kunmap(page);
152
153 checked:
154         afs_stat_v(dvnode, n_read_dir);
155         return true;
156
157 error:
158         return false;
159 }
160
161 /*
162  * Check the contents of a directory that we've just read.
163  */
164 static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
165 {
166         struct afs_xdr_dir_page *dbuf;
167         unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
168
169         for (i = 0; i < req->nr_pages; i++)
170                 if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
171                         goto bad;
172         return true;
173
174 bad:
175         pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
176                 dvnode->fid.vid, dvnode->fid.vnode,
177                 req->file_size, req->len, req->actual_len, req->remain);
178         pr_warn("DIR %llx %x %x %x\n",
179                 req->pos, req->index, req->nr_pages, req->offset);
180
181         for (i = 0; i < req->nr_pages; i++) {
182                 dbuf = kmap(req->pages[i]);
183                 for (j = 0; j < qty; j++) {
184                         union afs_xdr_dir_block *block = &dbuf->blocks[j];
185
186                         pr_warn("[%02x] %32phN\n", i * qty + j, block);
187                 }
188                 kunmap(req->pages[i]);
189         }
190         return false;
191 }
192
193 /*
194  * open an AFS directory file
195  */
196 static int afs_dir_open(struct inode *inode, struct file *file)
197 {
198         _enter("{%lu}", inode->i_ino);
199
200         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
201         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
202
203         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
204                 return -ENOENT;
205
206         return afs_open(inode, file);
207 }
208
209 /*
210  * Read the directory into the pagecache in one go, scrubbing the previous
211  * contents.  The list of pages is returned, pinning them so that they don't
212  * get reclaimed during the iteration.
213  */
214 static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
215         __acquires(&dvnode->validate_lock)
216 {
217         struct afs_read *req;
218         loff_t i_size;
219         int nr_pages, nr_inline, i, n;
220         int ret = -ENOMEM;
221
222 retry:
223         i_size = i_size_read(&dvnode->vfs_inode);
224         if (i_size < 2048)
225                 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
226         if (i_size > 2048 * 1024) {
227                 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
228                 return ERR_PTR(-EFBIG);
229         }
230
231         _enter("%llu", i_size);
232
233         /* Get a request record to hold the page list.  We want to hold it
234          * inline if we can, but we don't want to make an order 1 allocation.
235          */
236         nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
237         nr_inline = nr_pages;
238         if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
239                 nr_inline = 0;
240
241         req = kzalloc(struct_size(req, array, nr_inline), GFP_KERNEL);
242         if (!req)
243                 return ERR_PTR(-ENOMEM);
244
245         refcount_set(&req->usage, 1);
246         req->nr_pages = nr_pages;
247         req->actual_len = i_size; /* May change */
248         req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
249         req->data_version = dvnode->status.data_version; /* May change */
250         if (nr_inline > 0) {
251                 req->pages = req->array;
252         } else {
253                 req->pages = kcalloc(nr_pages, sizeof(struct page *),
254                                      GFP_KERNEL);
255                 if (!req->pages)
256                         goto error;
257         }
258
259         /* Get a list of all the pages that hold or will hold the directory
260          * content.  We need to fill in any gaps that we might find where the
261          * memory reclaimer has been at work.  If there are any gaps, we will
262          * need to reread the entire directory contents.
263          */
264         i = 0;
265         do {
266                 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
267                                           req->nr_pages - i,
268                                           req->pages + i);
269                 _debug("find %u at %u/%u", n, i, req->nr_pages);
270                 if (n == 0) {
271                         gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
272
273                         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
274                                 afs_stat_v(dvnode, n_inval);
275
276                         ret = -ENOMEM;
277                         req->pages[i] = __page_cache_alloc(gfp);
278                         if (!req->pages[i])
279                                 goto error;
280                         ret = add_to_page_cache_lru(req->pages[i],
281                                                     dvnode->vfs_inode.i_mapping,
282                                                     i, gfp);
283                         if (ret < 0)
284                                 goto error;
285
286                         set_page_private(req->pages[i], 1);
287                         SetPagePrivate(req->pages[i]);
288                         unlock_page(req->pages[i]);
289                         i++;
290                 } else {
291                         i += n;
292                 }
293         } while (i < req->nr_pages);
294
295         /* If we're going to reload, we need to lock all the pages to prevent
296          * races.
297          */
298         ret = -ERESTARTSYS;
299         if (down_read_killable(&dvnode->validate_lock) < 0)
300                 goto error;
301
302         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
303                 goto success;
304
305         up_read(&dvnode->validate_lock);
306         if (down_write_killable(&dvnode->validate_lock) < 0)
307                 goto error;
308
309         if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
310                 trace_afs_reload_dir(dvnode);
311                 ret = afs_fetch_data(dvnode, key, req);
312                 if (ret < 0)
313                         goto error_unlock;
314
315                 task_io_account_read(PAGE_SIZE * req->nr_pages);
316
317                 if (req->len < req->file_size)
318                         goto content_has_grown;
319
320                 /* Validate the data we just read. */
321                 ret = -EIO;
322                 if (!afs_dir_check_pages(dvnode, req))
323                         goto error_unlock;
324
325                 // TODO: Trim excess pages
326
327                 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
328         }
329
330         downgrade_write(&dvnode->validate_lock);
331 success:
332         return req;
333
334 error_unlock:
335         up_write(&dvnode->validate_lock);
336 error:
337         afs_put_read(req);
338         _leave(" = %d", ret);
339         return ERR_PTR(ret);
340
341 content_has_grown:
342         up_write(&dvnode->validate_lock);
343         afs_put_read(req);
344         goto retry;
345 }
346
347 /*
348  * deal with one block in an AFS directory
349  */
350 static int afs_dir_iterate_block(struct afs_vnode *dvnode,
351                                  struct dir_context *ctx,
352                                  union afs_xdr_dir_block *block,
353                                  unsigned blkoff)
354 {
355         union afs_xdr_dirent *dire;
356         unsigned offset, next, curr;
357         size_t nlen;
358         int tmp;
359
360         _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
361
362         curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
363
364         /* walk through the block, an entry at a time */
365         for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
366              offset < AFS_DIR_SLOTS_PER_BLOCK;
367              offset = next
368              ) {
369                 next = offset + 1;
370
371                 /* skip entries marked unused in the bitmap */
372                 if (!(block->hdr.bitmap[offset / 8] &
373                       (1 << (offset % 8)))) {
374                         _debug("ENT[%zu.%u]: unused",
375                                blkoff / sizeof(union afs_xdr_dir_block), offset);
376                         if (offset >= curr)
377                                 ctx->pos = blkoff +
378                                         next * sizeof(union afs_xdr_dirent);
379                         continue;
380                 }
381
382                 /* got a valid entry */
383                 dire = &block->dirents[offset];
384                 nlen = strnlen(dire->u.name,
385                                sizeof(*block) -
386                                offset * sizeof(union afs_xdr_dirent));
387
388                 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
389                        blkoff / sizeof(union afs_xdr_dir_block), offset,
390                        (offset < curr ? "skip" : "fill"),
391                        nlen, dire->u.name);
392
393                 /* work out where the next possible entry is */
394                 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
395                         if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
396                                 _debug("ENT[%zu.%u]:"
397                                        " %u travelled beyond end dir block"
398                                        " (len %u/%zu)",
399                                        blkoff / sizeof(union afs_xdr_dir_block),
400                                        offset, next, tmp, nlen);
401                                 return afs_bad(dvnode, afs_file_error_dir_over_end);
402                         }
403                         if (!(block->hdr.bitmap[next / 8] &
404                               (1 << (next % 8)))) {
405                                 _debug("ENT[%zu.%u]:"
406                                        " %u unmarked extension (len %u/%zu)",
407                                        blkoff / sizeof(union afs_xdr_dir_block),
408                                        offset, next, tmp, nlen);
409                                 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
410                         }
411
412                         _debug("ENT[%zu.%u]: ext %u/%zu",
413                                blkoff / sizeof(union afs_xdr_dir_block),
414                                next, tmp, nlen);
415                         next++;
416                 }
417
418                 /* skip if starts before the current position */
419                 if (offset < curr)
420                         continue;
421
422                 /* found the next entry */
423                 if (!dir_emit(ctx, dire->u.name, nlen,
424                               ntohl(dire->u.vnode),
425                               (ctx->actor == afs_lookup_filldir ||
426                                ctx->actor == afs_lookup_one_filldir)?
427                               ntohl(dire->u.unique) : DT_UNKNOWN)) {
428                         _leave(" = 0 [full]");
429                         return 0;
430                 }
431
432                 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
433         }
434
435         _leave(" = 1 [more]");
436         return 1;
437 }
438
439 /*
440  * iterate through the data blob that lists the contents of an AFS directory
441  */
442 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
443                            struct key *key, afs_dataversion_t *_dir_version)
444 {
445         struct afs_vnode *dvnode = AFS_FS_I(dir);
446         struct afs_xdr_dir_page *dbuf;
447         union afs_xdr_dir_block *dblock;
448         struct afs_read *req;
449         struct page *page;
450         unsigned blkoff, limit;
451         int ret;
452
453         _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
454
455         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
456                 _leave(" = -ESTALE");
457                 return -ESTALE;
458         }
459
460         req = afs_read_dir(dvnode, key);
461         if (IS_ERR(req))
462                 return PTR_ERR(req);
463         *_dir_version = req->data_version;
464
465         /* round the file position up to the next entry boundary */
466         ctx->pos += sizeof(union afs_xdr_dirent) - 1;
467         ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
468
469         /* walk through the blocks in sequence */
470         ret = 0;
471         while (ctx->pos < req->actual_len) {
472                 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
473
474                 /* Fetch the appropriate page from the directory and re-add it
475                  * to the LRU.
476                  */
477                 page = req->pages[blkoff / PAGE_SIZE];
478                 if (!page) {
479                         ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
480                         break;
481                 }
482                 mark_page_accessed(page);
483
484                 limit = blkoff & ~(PAGE_SIZE - 1);
485
486                 dbuf = kmap(page);
487
488                 /* deal with the individual blocks stashed on this page */
489                 do {
490                         dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
491                                                sizeof(union afs_xdr_dir_block)];
492                         ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
493                         if (ret != 1) {
494                                 kunmap(page);
495                                 goto out;
496                         }
497
498                         blkoff += sizeof(union afs_xdr_dir_block);
499
500                 } while (ctx->pos < dir->i_size && blkoff < limit);
501
502                 kunmap(page);
503                 ret = 0;
504         }
505
506 out:
507         up_read(&dvnode->validate_lock);
508         afs_put_read(req);
509         _leave(" = %d", ret);
510         return ret;
511 }
512
513 /*
514  * read an AFS directory
515  */
516 static int afs_readdir(struct file *file, struct dir_context *ctx)
517 {
518         afs_dataversion_t dir_version;
519
520         return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
521                                &dir_version);
522 }
523
524 /*
525  * Search the directory for a single name
526  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
527  *   uniquifier through dtype
528  */
529 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530                                   int nlen, loff_t fpos, u64 ino, unsigned dtype)
531 {
532         struct afs_lookup_one_cookie *cookie =
533                 container_of(ctx, struct afs_lookup_one_cookie, ctx);
534
535         _enter("{%s,%u},%s,%u,,%llu,%u",
536                cookie->name.name, cookie->name.len, name, nlen,
537                (unsigned long long) ino, dtype);
538
539         /* insanity checks first */
540         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
541         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
542
543         if (cookie->name.len != nlen ||
544             memcmp(cookie->name.name, name, nlen) != 0) {
545                 _leave(" = 0 [no]");
546                 return 0;
547         }
548
549         cookie->fid.vnode = ino;
550         cookie->fid.unique = dtype;
551         cookie->found = 1;
552
553         _leave(" = -1 [found]");
554         return -1;
555 }
556
557 /*
558  * Do a lookup of a single name in a directory
559  * - just returns the FID the dentry name maps to if found
560  */
561 static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
562                              struct afs_fid *fid, struct key *key,
563                              afs_dataversion_t *_dir_version)
564 {
565         struct afs_super_info *as = dir->i_sb->s_fs_info;
566         struct afs_lookup_one_cookie cookie = {
567                 .ctx.actor = afs_lookup_one_filldir,
568                 .name = dentry->d_name,
569                 .fid.vid = as->volume->vid
570         };
571         int ret;
572
573         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
574
575         /* search the directory */
576         ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
577         if (ret < 0) {
578                 _leave(" = %d [iter]", ret);
579                 return ret;
580         }
581
582         ret = -ENOENT;
583         if (!cookie.found) {
584                 _leave(" = -ENOENT [not found]");
585                 return -ENOENT;
586         }
587
588         *fid = cookie.fid;
589         _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
590         return 0;
591 }
592
593 /*
594  * search the directory for a name
595  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
596  *   uniquifier through dtype
597  */
598 static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
599                               int nlen, loff_t fpos, u64 ino, unsigned dtype)
600 {
601         struct afs_lookup_cookie *cookie =
602                 container_of(ctx, struct afs_lookup_cookie, ctx);
603         int ret;
604
605         _enter("{%s,%u},%s,%u,,%llu,%u",
606                cookie->name.name, cookie->name.len, name, nlen,
607                (unsigned long long) ino, dtype);
608
609         /* insanity checks first */
610         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
611         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
612
613         if (cookie->found) {
614                 if (cookie->nr_fids < 50) {
615                         cookie->fids[cookie->nr_fids].vnode     = ino;
616                         cookie->fids[cookie->nr_fids].unique    = dtype;
617                         cookie->nr_fids++;
618                 }
619         } else if (cookie->name.len == nlen &&
620                    memcmp(cookie->name.name, name, nlen) == 0) {
621                 cookie->fids[0].vnode   = ino;
622                 cookie->fids[0].unique  = dtype;
623                 cookie->found = 1;
624                 if (cookie->one_only)
625                         return -1;
626         }
627
628         ret = cookie->nr_fids >= 50 ? -1 : 0;
629         _leave(" = %d", ret);
630         return ret;
631 }
632
633 /*
634  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
635  * files in one go and create inodes for them.  The inode of the file we were
636  * asked for is returned.
637  */
638 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
639                                    struct key *key)
640 {
641         struct afs_lookup_cookie *cookie;
642         struct afs_cb_interest *dcbi, *cbi = NULL;
643         struct afs_super_info *as = dir->i_sb->s_fs_info;
644         struct afs_status_cb *scb;
645         struct afs_iget_data iget_data;
646         struct afs_fs_cursor fc;
647         struct afs_server *server;
648         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
649         struct inode *inode = NULL, *ti;
650         afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
651         int ret, i;
652
653         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
654
655         cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
656         if (!cookie)
657                 return ERR_PTR(-ENOMEM);
658
659         cookie->ctx.actor = afs_lookup_filldir;
660         cookie->name = dentry->d_name;
661         cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
662
663         read_seqlock_excl(&dvnode->cb_lock);
664         dcbi = rcu_dereference_protected(dvnode->cb_interest,
665                                          lockdep_is_held(&dvnode->cb_lock.lock));
666         if (dcbi) {
667                 server = dcbi->server;
668                 if (server &&
669                     test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
670                         cookie->one_only = true;
671         }
672         read_sequnlock_excl(&dvnode->cb_lock);
673
674         for (i = 0; i < 50; i++)
675                 cookie->fids[i].vid = as->volume->vid;
676
677         /* search the directory */
678         ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
679         if (ret < 0) {
680                 inode = ERR_PTR(ret);
681                 goto out;
682         }
683
684         dentry->d_fsdata = (void *)(unsigned long)data_version;
685
686         inode = ERR_PTR(-ENOENT);
687         if (!cookie->found)
688                 goto out;
689
690         /* Check to see if we already have an inode for the primary fid. */
691         iget_data.fid = cookie->fids[0];
692         iget_data.volume = dvnode->volume;
693         iget_data.cb_v_break = dvnode->volume->cb_v_break;
694         iget_data.cb_s_break = 0;
695         inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
696                          afs_iget5_test, &iget_data);
697         if (inode)
698                 goto out;
699
700         /* Need space for examining all the selected files */
701         inode = ERR_PTR(-ENOMEM);
702         cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
703                                     GFP_KERNEL);
704         if (!cookie->statuses)
705                 goto out;
706
707         cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *),
708                                  GFP_KERNEL);
709         if (!cookie->inodes)
710                 goto out_s;
711
712         for (i = 1; i < cookie->nr_fids; i++) {
713                 scb = &cookie->statuses[i];
714
715                 /* Find any inodes that already exist and get their
716                  * callback counters.
717                  */
718                 iget_data.fid = cookie->fids[i];
719                 ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode,
720                                      afs_iget5_test, &iget_data);
721                 if (!IS_ERR_OR_NULL(ti)) {
722                         vnode = AFS_FS_I(ti);
723                         scb->cb_break = afs_calc_vnode_cb_break(vnode);
724                         cookie->inodes[i] = ti;
725                 }
726         }
727
728         /* Try FS.InlineBulkStatus first.  Abort codes for the individual
729          * lookups contained therein are stored in the reply without aborting
730          * the whole operation.
731          */
732         if (cookie->one_only)
733                 goto no_inline_bulk_status;
734
735         inode = ERR_PTR(-ERESTARTSYS);
736         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
737                 while (afs_select_fileserver(&fc)) {
738                         if (test_bit(AFS_SERVER_FL_NO_IBULK,
739                                       &fc.cbi->server->flags)) {
740                                 fc.ac.abort_code = RX_INVALID_OPERATION;
741                                 fc.ac.error = -ECONNABORTED;
742                                 break;
743                         }
744                         iget_data.cb_v_break = dvnode->volume->cb_v_break;
745                         iget_data.cb_s_break = fc.cbi->server->cb_s_break;
746                         afs_fs_inline_bulk_status(&fc,
747                                                   afs_v2net(dvnode),
748                                                   cookie->fids,
749                                                   cookie->statuses,
750                                                   cookie->nr_fids, NULL);
751                 }
752
753                 if (fc.ac.error == 0)
754                         cbi = afs_get_cb_interest(fc.cbi);
755                 if (fc.ac.abort_code == RX_INVALID_OPERATION)
756                         set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
757                 inode = ERR_PTR(afs_end_vnode_operation(&fc));
758         }
759
760         if (!IS_ERR(inode))
761                 goto success;
762         if (fc.ac.abort_code != RX_INVALID_OPERATION)
763                 goto out_c;
764
765 no_inline_bulk_status:
766         /* We could try FS.BulkStatus next, but this aborts the entire op if
767          * any of the lookups fails - so, for the moment, revert to
768          * FS.FetchStatus for just the primary fid.
769          */
770         inode = ERR_PTR(-ERESTARTSYS);
771         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
772                 while (afs_select_fileserver(&fc)) {
773                         iget_data.cb_v_break = dvnode->volume->cb_v_break;
774                         iget_data.cb_s_break = fc.cbi->server->cb_s_break;
775                         scb = &cookie->statuses[0];
776                         afs_fs_fetch_status(&fc,
777                                             afs_v2net(dvnode),
778                                             cookie->fids,
779                                             scb,
780                                             NULL);
781                 }
782
783                 if (fc.ac.error == 0)
784                         cbi = afs_get_cb_interest(fc.cbi);
785                 inode = ERR_PTR(afs_end_vnode_operation(&fc));
786         }
787
788         if (IS_ERR(inode))
789                 goto out_c;
790
791 success:
792         /* Turn all the files into inodes and save the first one - which is the
793          * one we actually want.
794          */
795         scb = &cookie->statuses[0];
796         if (scb->status.abort_code != 0)
797                 inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
798
799         for (i = 0; i < cookie->nr_fids; i++) {
800                 struct afs_status_cb *scb = &cookie->statuses[i];
801
802                 if (!scb->have_status && !scb->have_error)
803                         continue;
804
805                 if (cookie->inodes[i]) {
806                         afs_vnode_commit_status(&fc, AFS_FS_I(cookie->inodes[i]),
807                                                 scb->cb_break, NULL, scb);
808                         continue;
809                 }
810
811                 if (scb->status.abort_code != 0)
812                         continue;
813
814                 iget_data.fid = cookie->fids[i];
815                 ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
816                 if (!IS_ERR(ti))
817                         afs_cache_permit(AFS_FS_I(ti), key,
818                                          0 /* Assume vnode->cb_break is 0 */ +
819                                          iget_data.cb_v_break,
820                                          scb);
821                 if (i == 0) {
822                         inode = ti;
823                 } else {
824                         if (!IS_ERR(ti))
825                                 iput(ti);
826                 }
827         }
828
829 out_c:
830         afs_put_cb_interest(afs_v2net(dvnode), cbi);
831         if (cookie->inodes) {
832                 for (i = 0; i < cookie->nr_fids; i++)
833                         iput(cookie->inodes[i]);
834                 kfree(cookie->inodes);
835         }
836 out_s:
837         kvfree(cookie->statuses);
838 out:
839         kfree(cookie);
840         return inode;
841 }
842
843 /*
844  * Look up an entry in a directory with @sys substitution.
845  */
846 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
847                                        struct key *key)
848 {
849         struct afs_sysnames *subs;
850         struct afs_net *net = afs_i2net(dir);
851         struct dentry *ret;
852         char *buf, *p, *name;
853         int len, i;
854
855         _enter("");
856
857         ret = ERR_PTR(-ENOMEM);
858         p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
859         if (!buf)
860                 goto out_p;
861         if (dentry->d_name.len > 4) {
862                 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
863                 p += dentry->d_name.len - 4;
864         }
865
866         /* There is an ordered list of substitutes that we have to try. */
867         read_lock(&net->sysnames_lock);
868         subs = net->sysnames;
869         refcount_inc(&subs->usage);
870         read_unlock(&net->sysnames_lock);
871
872         for (i = 0; i < subs->nr; i++) {
873                 name = subs->subs[i];
874                 len = dentry->d_name.len - 4 + strlen(name);
875                 if (len >= AFSNAMEMAX) {
876                         ret = ERR_PTR(-ENAMETOOLONG);
877                         goto out_s;
878                 }
879
880                 strcpy(p, name);
881                 ret = lookup_one_len(buf, dentry->d_parent, len);
882                 if (IS_ERR(ret) || d_is_positive(ret))
883                         goto out_s;
884                 dput(ret);
885         }
886
887         /* We don't want to d_add() the @sys dentry here as we don't want to
888          * the cached dentry to hide changes to the sysnames list.
889          */
890         ret = NULL;
891 out_s:
892         afs_put_sysnames(subs);
893         kfree(buf);
894 out_p:
895         key_put(key);
896         return ret;
897 }
898
899 /*
900  * look up an entry in a directory
901  */
902 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
903                                  unsigned int flags)
904 {
905         struct afs_vnode *dvnode = AFS_FS_I(dir);
906         struct inode *inode;
907         struct dentry *d;
908         struct key *key;
909         int ret;
910
911         _enter("{%llx:%llu},%p{%pd},",
912                dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
913
914         ASSERTCMP(d_inode(dentry), ==, NULL);
915
916         if (dentry->d_name.len >= AFSNAMEMAX) {
917                 _leave(" = -ENAMETOOLONG");
918                 return ERR_PTR(-ENAMETOOLONG);
919         }
920
921         if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
922                 _leave(" = -ESTALE");
923                 return ERR_PTR(-ESTALE);
924         }
925
926         key = afs_request_key(dvnode->volume->cell);
927         if (IS_ERR(key)) {
928                 _leave(" = %ld [key]", PTR_ERR(key));
929                 return ERR_CAST(key);
930         }
931
932         ret = afs_validate(dvnode, key);
933         if (ret < 0) {
934                 key_put(key);
935                 _leave(" = %d [val]", ret);
936                 return ERR_PTR(ret);
937         }
938
939         if (dentry->d_name.len >= 4 &&
940             dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
941             dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
942             dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
943             dentry->d_name.name[dentry->d_name.len - 1] == 's')
944                 return afs_lookup_atsys(dir, dentry, key);
945
946         afs_stat_v(dvnode, n_lookup);
947         inode = afs_do_lookup(dir, dentry, key);
948         key_put(key);
949         if (inode == ERR_PTR(-ENOENT)) {
950                 inode = afs_try_auto_mntpt(dentry, dir);
951         } else {
952                 dentry->d_fsdata =
953                         (void *)(unsigned long)dvnode->status.data_version;
954         }
955         d = d_splice_alias(inode, dentry);
956         if (!IS_ERR_OR_NULL(d)) {
957                 d->d_fsdata = dentry->d_fsdata;
958                 trace_afs_lookup(dvnode, &d->d_name,
959                                  inode ? AFS_FS_I(inode) : NULL);
960         } else {
961                 trace_afs_lookup(dvnode, &dentry->d_name,
962                                  inode ? AFS_FS_I(inode) : NULL);
963         }
964         return d;
965 }
966
967 /*
968  * check that a dentry lookup hit has found a valid entry
969  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
970  *   inode
971  */
972 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
973 {
974         struct afs_vnode *vnode, *dir;
975         struct afs_fid uninitialized_var(fid);
976         struct dentry *parent;
977         struct inode *inode;
978         struct key *key;
979         afs_dataversion_t dir_version;
980         long de_version;
981         int ret;
982
983         if (flags & LOOKUP_RCU)
984                 return -ECHILD;
985
986         if (d_really_is_positive(dentry)) {
987                 vnode = AFS_FS_I(d_inode(dentry));
988                 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
989                        vnode->fid.vid, vnode->fid.vnode, dentry,
990                        vnode->flags);
991         } else {
992                 _enter("{neg n=%pd}", dentry);
993         }
994
995         key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
996         if (IS_ERR(key))
997                 key = NULL;
998
999         if (d_really_is_positive(dentry)) {
1000                 inode = d_inode(dentry);
1001                 if (inode) {
1002                         vnode = AFS_FS_I(inode);
1003                         afs_validate(vnode, key);
1004                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1005                                 goto out_bad;
1006                 }
1007         }
1008
1009         /* lock down the parent dentry so we can peer at it */
1010         parent = dget_parent(dentry);
1011         dir = AFS_FS_I(d_inode(parent));
1012
1013         /* validate the parent directory */
1014         afs_validate(dir, key);
1015
1016         if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1017                 _debug("%pd: parent dir deleted", dentry);
1018                 goto out_bad_parent;
1019         }
1020
1021         /* We only need to invalidate a dentry if the server's copy changed
1022          * behind our back.  If we made the change, it's no problem.  Note that
1023          * on a 32-bit system, we only have 32 bits in the dentry to store the
1024          * version.
1025          */
1026         dir_version = dir->status.data_version;
1027         de_version = (long)dentry->d_fsdata;
1028         if (de_version == (long)dir_version)
1029                 goto out_valid_noupdate;
1030
1031         dir_version = dir->invalid_before;
1032         if (de_version - (long)dir_version >= 0)
1033                 goto out_valid;
1034
1035         _debug("dir modified");
1036         afs_stat_v(dir, n_reval);
1037
1038         /* search the directory for this vnode */
1039         ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version);
1040         switch (ret) {
1041         case 0:
1042                 /* the filename maps to something */
1043                 if (d_really_is_negative(dentry))
1044                         goto out_bad_parent;
1045                 inode = d_inode(dentry);
1046                 if (is_bad_inode(inode)) {
1047                         printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1048                                dentry);
1049                         goto out_bad_parent;
1050                 }
1051
1052                 vnode = AFS_FS_I(inode);
1053
1054                 /* if the vnode ID has changed, then the dirent points to a
1055                  * different file */
1056                 if (fid.vnode != vnode->fid.vnode) {
1057                         _debug("%pd: dirent changed [%llu != %llu]",
1058                                dentry, fid.vnode,
1059                                vnode->fid.vnode);
1060                         goto not_found;
1061                 }
1062
1063                 /* if the vnode ID uniqifier has changed, then the file has
1064                  * been deleted and replaced, and the original vnode ID has
1065                  * been reused */
1066                 if (fid.unique != vnode->fid.unique) {
1067                         _debug("%pd: file deleted (uq %u -> %u I:%u)",
1068                                dentry, fid.unique,
1069                                vnode->fid.unique,
1070                                vnode->vfs_inode.i_generation);
1071                         write_seqlock(&vnode->cb_lock);
1072                         set_bit(AFS_VNODE_DELETED, &vnode->flags);
1073                         write_sequnlock(&vnode->cb_lock);
1074                         goto not_found;
1075                 }
1076                 goto out_valid;
1077
1078         case -ENOENT:
1079                 /* the filename is unknown */
1080                 _debug("%pd: dirent not found", dentry);
1081                 if (d_really_is_positive(dentry))
1082                         goto not_found;
1083                 goto out_valid;
1084
1085         default:
1086                 _debug("failed to iterate dir %pd: %d",
1087                        parent, ret);
1088                 goto out_bad_parent;
1089         }
1090
1091 out_valid:
1092         dentry->d_fsdata = (void *)(unsigned long)dir_version;
1093 out_valid_noupdate:
1094         dput(parent);
1095         key_put(key);
1096         _leave(" = 1 [valid]");
1097         return 1;
1098
1099         /* the dirent, if it exists, now points to a different vnode */
1100 not_found:
1101         spin_lock(&dentry->d_lock);
1102         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1103         spin_unlock(&dentry->d_lock);
1104
1105 out_bad_parent:
1106         _debug("dropping dentry %pd2", dentry);
1107         dput(parent);
1108 out_bad:
1109         key_put(key);
1110
1111         _leave(" = 0 [bad]");
1112         return 0;
1113 }
1114
1115 /*
1116  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1117  * sleep)
1118  * - called from dput() when d_count is going to 0.
1119  * - return 1 to request dentry be unhashed, 0 otherwise
1120  */
1121 static int afs_d_delete(const struct dentry *dentry)
1122 {
1123         _enter("%pd", dentry);
1124
1125         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1126                 goto zap;
1127
1128         if (d_really_is_positive(dentry) &&
1129             (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
1130              test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1131                 goto zap;
1132
1133         _leave(" = 0 [keep]");
1134         return 0;
1135
1136 zap:
1137         _leave(" = 1 [zap]");
1138         return 1;
1139 }
1140
1141 /*
1142  * Clean up sillyrename files on dentry removal.
1143  */
1144 static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1145 {
1146         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1147                 afs_silly_iput(dentry, inode);
1148         iput(inode);
1149 }
1150
1151 /*
1152  * handle dentry release
1153  */
1154 void afs_d_release(struct dentry *dentry)
1155 {
1156         _enter("%pd", dentry);
1157 }
1158
1159 /*
1160  * Create a new inode for create/mkdir/symlink
1161  */
1162 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1163                                 struct dentry *new_dentry,
1164                                 struct afs_iget_data *new_data,
1165                                 struct afs_status_cb *new_scb)
1166 {
1167         struct afs_vnode *vnode;
1168         struct inode *inode;
1169
1170         if (fc->ac.error < 0)
1171                 return;
1172
1173         inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1174                          new_data, new_scb, fc->cbi, fc->vnode);
1175         if (IS_ERR(inode)) {
1176                 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1177                  * the new directory on the server.
1178                  */
1179                 fc->ac.error = PTR_ERR(inode);
1180                 return;
1181         }
1182
1183         vnode = AFS_FS_I(inode);
1184         set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1185         if (fc->ac.error == 0)
1186                 afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
1187         d_instantiate(new_dentry, inode);
1188 }
1189
1190 static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1191                                    struct afs_iget_data *iget_data)
1192 {
1193         iget_data->volume = fc->vnode->volume;
1194         iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1195         iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1196 }
1197
1198 /*
1199  * Note that a dentry got changed.  We need to set d_fsdata to the data version
1200  * number derived from the result of the operation.  It doesn't matter if
1201  * d_fsdata goes backwards as we'll just revalidate.
1202  */
1203 static void afs_update_dentry_version(struct afs_fs_cursor *fc,
1204                                       struct dentry *dentry,
1205                                       struct afs_status_cb *scb)
1206 {
1207         if (fc->ac.error == 0)
1208                 dentry->d_fsdata =
1209                         (void *)(unsigned long)scb->status.data_version;
1210 }
1211
1212 /*
1213  * create a directory on an AFS filesystem
1214  */
1215 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1216 {
1217         struct afs_iget_data iget_data;
1218         struct afs_status_cb *scb;
1219         struct afs_fs_cursor fc;
1220         struct afs_vnode *dvnode = AFS_FS_I(dir);
1221         struct key *key;
1222         int ret;
1223
1224         mode |= S_IFDIR;
1225
1226         _enter("{%llx:%llu},{%pd},%ho",
1227                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1228
1229         ret = -ENOMEM;
1230         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1231         if (!scb)
1232                 goto error;
1233
1234         key = afs_request_key(dvnode->volume->cell);
1235         if (IS_ERR(key)) {
1236                 ret = PTR_ERR(key);
1237                 goto error_scb;
1238         }
1239
1240         ret = -ERESTARTSYS;
1241         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1242                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1243
1244                 while (afs_select_fileserver(&fc)) {
1245                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1246                         afs_prep_for_new_inode(&fc, &iget_data);
1247                         afs_fs_create(&fc, dentry->d_name.name, mode,
1248                                       &scb[0], &iget_data.fid, &scb[1]);
1249                 }
1250
1251                 afs_check_for_remote_deletion(&fc, dvnode);
1252                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1253                                         &data_version, &scb[0]);
1254                 afs_update_dentry_version(&fc, dentry, &scb[0]);
1255                 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1256                 ret = afs_end_vnode_operation(&fc);
1257                 if (ret < 0)
1258                         goto error_key;
1259         } else {
1260                 goto error_key;
1261         }
1262
1263         if (ret == 0 &&
1264             test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1265                 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1266                                  afs_edit_dir_for_create);
1267
1268         key_put(key);
1269         kfree(scb);
1270         _leave(" = 0");
1271         return 0;
1272
1273 error_key:
1274         key_put(key);
1275 error_scb:
1276         kfree(scb);
1277 error:
1278         d_drop(dentry);
1279         _leave(" = %d", ret);
1280         return ret;
1281 }
1282
1283 /*
1284  * Remove a subdir from a directory.
1285  */
1286 static void afs_dir_remove_subdir(struct dentry *dentry)
1287 {
1288         if (d_really_is_positive(dentry)) {
1289                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1290
1291                 clear_nlink(&vnode->vfs_inode);
1292                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1293                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1294                 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1295         }
1296 }
1297
1298 /*
1299  * remove a directory from an AFS filesystem
1300  */
1301 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1302 {
1303         struct afs_status_cb *scb;
1304         struct afs_fs_cursor fc;
1305         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1306         struct key *key;
1307         int ret;
1308
1309         _enter("{%llx:%llu},{%pd}",
1310                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1311
1312         scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1313         if (!scb)
1314                 return -ENOMEM;
1315
1316         key = afs_request_key(dvnode->volume->cell);
1317         if (IS_ERR(key)) {
1318                 ret = PTR_ERR(key);
1319                 goto error;
1320         }
1321
1322         /* Try to make sure we have a callback promise on the victim. */
1323         if (d_really_is_positive(dentry)) {
1324                 vnode = AFS_FS_I(d_inode(dentry));
1325                 ret = afs_validate(vnode, key);
1326                 if (ret < 0)
1327                         goto error_key;
1328         }
1329
1330         if (vnode) {
1331                 ret = down_write_killable(&vnode->rmdir_lock);
1332                 if (ret < 0)
1333                         goto error_key;
1334         }
1335
1336         ret = -ERESTARTSYS;
1337         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1338                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1339
1340                 while (afs_select_fileserver(&fc)) {
1341                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1342                         afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1343                 }
1344
1345                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1346                                         &data_version, scb);
1347                 afs_update_dentry_version(&fc, dentry, scb);
1348                 ret = afs_end_vnode_operation(&fc);
1349                 if (ret == 0) {
1350                         afs_dir_remove_subdir(dentry);
1351                         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1352                                 afs_edit_dir_remove(dvnode, &dentry->d_name,
1353                                                     afs_edit_dir_for_rmdir);
1354                 }
1355         }
1356
1357         if (vnode)
1358                 up_write(&vnode->rmdir_lock);
1359 error_key:
1360         key_put(key);
1361 error:
1362         kfree(scb);
1363         return ret;
1364 }
1365
1366 /*
1367  * Remove a link to a file or symlink from a directory.
1368  *
1369  * If the file was not deleted due to excess hard links, the fileserver will
1370  * break the callback promise on the file - if it had one - before it returns
1371  * to us, and if it was deleted, it won't
1372  *
1373  * However, if we didn't have a callback promise outstanding, or it was
1374  * outstanding on a different server, then it won't break it either...
1375  */
1376 static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1377                                struct key *key)
1378 {
1379         int ret = 0;
1380
1381         if (d_really_is_positive(dentry)) {
1382                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1383
1384                 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1385                         /* Already done */
1386                 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1387                         write_seqlock(&vnode->cb_lock);
1388                         drop_nlink(&vnode->vfs_inode);
1389                         if (vnode->vfs_inode.i_nlink == 0) {
1390                                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1391                                 __afs_break_callback(vnode, afs_cb_break_for_unlink);
1392                         }
1393                         write_sequnlock(&vnode->cb_lock);
1394                         ret = 0;
1395                 } else {
1396                         afs_break_callback(vnode, afs_cb_break_for_unlink);
1397
1398                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1399                                 kdebug("AFS_VNODE_DELETED");
1400
1401                         ret = afs_validate(vnode, key);
1402                         if (ret == -ESTALE)
1403                                 ret = 0;
1404                 }
1405                 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1406         }
1407
1408         return ret;
1409 }
1410
1411 /*
1412  * Remove a file or symlink from an AFS filesystem.
1413  */
1414 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1415 {
1416         struct afs_fs_cursor fc;
1417         struct afs_status_cb *scb;
1418         struct afs_vnode *dvnode = AFS_FS_I(dir);
1419         struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1420         struct key *key;
1421         bool need_rehash = false;
1422         int ret;
1423
1424         _enter("{%llx:%llu},{%pd}",
1425                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1426
1427         if (dentry->d_name.len >= AFSNAMEMAX)
1428                 return -ENAMETOOLONG;
1429
1430         ret = -ENOMEM;
1431         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1432         if (!scb)
1433                 goto error;
1434
1435         key = afs_request_key(dvnode->volume->cell);
1436         if (IS_ERR(key)) {
1437                 ret = PTR_ERR(key);
1438                 goto error_scb;
1439         }
1440
1441         /* Try to make sure we have a callback promise on the victim. */
1442         ret = afs_validate(vnode, key);
1443         if (ret < 0)
1444                 goto error_key;
1445
1446         spin_lock(&dentry->d_lock);
1447         if (d_count(dentry) > 1) {
1448                 spin_unlock(&dentry->d_lock);
1449                 /* Start asynchronous writeout of the inode */
1450                 write_inode_now(d_inode(dentry), 0);
1451                 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1452                 goto error_key;
1453         }
1454         if (!d_unhashed(dentry)) {
1455                 /* Prevent a race with RCU lookup. */
1456                 __d_drop(dentry);
1457                 need_rehash = true;
1458         }
1459         spin_unlock(&dentry->d_lock);
1460
1461         ret = -ERESTARTSYS;
1462         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1463                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1464                 afs_dataversion_t data_version_2 = vnode->status.data_version;
1465
1466                 while (afs_select_fileserver(&fc)) {
1467                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1468                         fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1469
1470                         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1471                             !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1472                                 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1473                                                     &scb[0], &scb[1]);
1474                                 if (fc.ac.error != -ECONNABORTED ||
1475                                     fc.ac.abort_code != RXGEN_OPCODE)
1476                                         continue;
1477                                 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1478                         }
1479
1480                         afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1481                 }
1482
1483                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1484                                         &data_version, &scb[0]);
1485                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1486                                         &data_version_2, &scb[1]);
1487                 afs_update_dentry_version(&fc, dentry, &scb[0]);
1488                 ret = afs_end_vnode_operation(&fc);
1489                 if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1490                         ret = afs_dir_remove_link(dvnode, dentry, key);
1491                 if (ret == 0 &&
1492                     test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1493                         afs_edit_dir_remove(dvnode, &dentry->d_name,
1494                                             afs_edit_dir_for_unlink);
1495         }
1496
1497         if (need_rehash && ret < 0 && ret != -ENOENT)
1498                 d_rehash(dentry);
1499
1500 error_key:
1501         key_put(key);
1502 error_scb:
1503         kfree(scb);
1504 error:
1505         _leave(" = %d", ret);
1506         return ret;
1507 }
1508
1509 /*
1510  * create a regular file on an AFS filesystem
1511  */
1512 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1513                       bool excl)
1514 {
1515         struct afs_iget_data iget_data;
1516         struct afs_fs_cursor fc;
1517         struct afs_status_cb *scb;
1518         struct afs_vnode *dvnode = AFS_FS_I(dir);
1519         struct key *key;
1520         int ret;
1521
1522         mode |= S_IFREG;
1523
1524         _enter("{%llx:%llu},{%pd},%ho,",
1525                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1526
1527         ret = -ENAMETOOLONG;
1528         if (dentry->d_name.len >= AFSNAMEMAX)
1529                 goto error;
1530
1531         key = afs_request_key(dvnode->volume->cell);
1532         if (IS_ERR(key)) {
1533                 ret = PTR_ERR(key);
1534                 goto error;
1535         }
1536
1537         ret = -ENOMEM;
1538         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1539         if (!scb)
1540                 goto error_scb;
1541
1542         ret = -ERESTARTSYS;
1543         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1544                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1545
1546                 while (afs_select_fileserver(&fc)) {
1547                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1548                         afs_prep_for_new_inode(&fc, &iget_data);
1549                         afs_fs_create(&fc, dentry->d_name.name, mode,
1550                                       &scb[0], &iget_data.fid, &scb[1]);
1551                 }
1552
1553                 afs_check_for_remote_deletion(&fc, dvnode);
1554                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1555                                         &data_version, &scb[0]);
1556                 afs_update_dentry_version(&fc, dentry, &scb[0]);
1557                 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1558                 ret = afs_end_vnode_operation(&fc);
1559                 if (ret < 0)
1560                         goto error_key;
1561         } else {
1562                 goto error_key;
1563         }
1564
1565         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1566                 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1567                                  afs_edit_dir_for_create);
1568
1569         kfree(scb);
1570         key_put(key);
1571         _leave(" = 0");
1572         return 0;
1573
1574 error_scb:
1575         kfree(scb);
1576 error_key:
1577         key_put(key);
1578 error:
1579         d_drop(dentry);
1580         _leave(" = %d", ret);
1581         return ret;
1582 }
1583
1584 /*
1585  * create a hard link between files in an AFS filesystem
1586  */
1587 static int afs_link(struct dentry *from, struct inode *dir,
1588                     struct dentry *dentry)
1589 {
1590         struct afs_fs_cursor fc;
1591         struct afs_status_cb *scb;
1592         struct afs_vnode *dvnode = AFS_FS_I(dir);
1593         struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1594         struct key *key;
1595         int ret;
1596
1597         _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1598                vnode->fid.vid, vnode->fid.vnode,
1599                dvnode->fid.vid, dvnode->fid.vnode,
1600                dentry);
1601
1602         ret = -ENAMETOOLONG;
1603         if (dentry->d_name.len >= AFSNAMEMAX)
1604                 goto error;
1605
1606         ret = -ENOMEM;
1607         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1608         if (!scb)
1609                 goto error;
1610
1611         key = afs_request_key(dvnode->volume->cell);
1612         if (IS_ERR(key)) {
1613                 ret = PTR_ERR(key);
1614                 goto error_scb;
1615         }
1616
1617         ret = -ERESTARTSYS;
1618         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1619                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1620
1621                 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1622                         afs_end_vnode_operation(&fc);
1623                         goto error_key;
1624                 }
1625
1626                 while (afs_select_fileserver(&fc)) {
1627                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1628                         fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1629                         afs_fs_link(&fc, vnode, dentry->d_name.name,
1630                                     &scb[0], &scb[1]);
1631                 }
1632
1633                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1634                                         &data_version, &scb[0]);
1635                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1636                                         NULL, &scb[1]);
1637                 ihold(&vnode->vfs_inode);
1638                 afs_update_dentry_version(&fc, dentry, &scb[0]);
1639                 d_instantiate(dentry, &vnode->vfs_inode);
1640
1641                 mutex_unlock(&vnode->io_lock);
1642                 ret = afs_end_vnode_operation(&fc);
1643                 if (ret < 0)
1644                         goto error_key;
1645         } else {
1646                 goto error_key;
1647         }
1648
1649         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1650                 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1651                                  afs_edit_dir_for_link);
1652
1653         key_put(key);
1654         kfree(scb);
1655         _leave(" = 0");
1656         return 0;
1657
1658 error_key:
1659         key_put(key);
1660 error_scb:
1661         kfree(scb);
1662 error:
1663         d_drop(dentry);
1664         _leave(" = %d", ret);
1665         return ret;
1666 }
1667
1668 /*
1669  * create a symlink in an AFS filesystem
1670  */
1671 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1672                        const char *content)
1673 {
1674         struct afs_iget_data iget_data;
1675         struct afs_fs_cursor fc;
1676         struct afs_status_cb *scb;
1677         struct afs_vnode *dvnode = AFS_FS_I(dir);
1678         struct key *key;
1679         int ret;
1680
1681         _enter("{%llx:%llu},{%pd},%s",
1682                dvnode->fid.vid, dvnode->fid.vnode, dentry,
1683                content);
1684
1685         ret = -ENAMETOOLONG;
1686         if (dentry->d_name.len >= AFSNAMEMAX)
1687                 goto error;
1688
1689         ret = -EINVAL;
1690         if (strlen(content) >= AFSPATHMAX)
1691                 goto error;
1692
1693         ret = -ENOMEM;
1694         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1695         if (!scb)
1696                 goto error;
1697
1698         key = afs_request_key(dvnode->volume->cell);
1699         if (IS_ERR(key)) {
1700                 ret = PTR_ERR(key);
1701                 goto error_scb;
1702         }
1703
1704         ret = -ERESTARTSYS;
1705         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1706                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1707
1708                 while (afs_select_fileserver(&fc)) {
1709                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1710                         afs_prep_for_new_inode(&fc, &iget_data);
1711                         afs_fs_symlink(&fc, dentry->d_name.name, content,
1712                                        &scb[0], &iget_data.fid, &scb[1]);
1713                 }
1714
1715                 afs_check_for_remote_deletion(&fc, dvnode);
1716                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1717                                         &data_version, &scb[0]);
1718                 afs_update_dentry_version(&fc, dentry, &scb[0]);
1719                 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1720                 ret = afs_end_vnode_operation(&fc);
1721                 if (ret < 0)
1722                         goto error_key;
1723         } else {
1724                 goto error_key;
1725         }
1726
1727         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1728                 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1729                                  afs_edit_dir_for_symlink);
1730
1731         key_put(key);
1732         kfree(scb);
1733         _leave(" = 0");
1734         return 0;
1735
1736 error_key:
1737         key_put(key);
1738 error_scb:
1739         kfree(scb);
1740 error:
1741         d_drop(dentry);
1742         _leave(" = %d", ret);
1743         return ret;
1744 }
1745
1746 /*
1747  * rename a file in an AFS filesystem and/or move it between directories
1748  */
1749 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1750                       struct inode *new_dir, struct dentry *new_dentry,
1751                       unsigned int flags)
1752 {
1753         struct afs_fs_cursor fc;
1754         struct afs_status_cb *scb;
1755         struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1756         struct dentry *tmp = NULL, *rehash = NULL;
1757         struct inode *new_inode;
1758         struct key *key;
1759         bool new_negative = d_is_negative(new_dentry);
1760         int ret;
1761
1762         if (flags)
1763                 return -EINVAL;
1764
1765         /* Don't allow silly-rename files be moved around. */
1766         if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1767                 return -EINVAL;
1768
1769         vnode = AFS_FS_I(d_inode(old_dentry));
1770         orig_dvnode = AFS_FS_I(old_dir);
1771         new_dvnode = AFS_FS_I(new_dir);
1772
1773         _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1774                orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1775                vnode->fid.vid, vnode->fid.vnode,
1776                new_dvnode->fid.vid, new_dvnode->fid.vnode,
1777                new_dentry);
1778
1779         ret = -ENOMEM;
1780         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1781         if (!scb)
1782                 goto error;
1783
1784         key = afs_request_key(orig_dvnode->volume->cell);
1785         if (IS_ERR(key)) {
1786                 ret = PTR_ERR(key);
1787                 goto error_scb;
1788         }
1789
1790         /* For non-directories, check whether the target is busy and if so,
1791          * make a copy of the dentry and then do a silly-rename.  If the
1792          * silly-rename succeeds, the copied dentry is hashed and becomes the
1793          * new target.
1794          */
1795         if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1796                 /* To prevent any new references to the target during the
1797                  * rename, we unhash the dentry in advance.
1798                  */
1799                 if (!d_unhashed(new_dentry)) {
1800                         d_drop(new_dentry);
1801                         rehash = new_dentry;
1802                 }
1803
1804                 if (d_count(new_dentry) > 2) {
1805                         /* copy the target dentry's name */
1806                         ret = -ENOMEM;
1807                         tmp = d_alloc(new_dentry->d_parent,
1808                                       &new_dentry->d_name);
1809                         if (!tmp)
1810                                 goto error_rehash;
1811
1812                         ret = afs_sillyrename(new_dvnode,
1813                                               AFS_FS_I(d_inode(new_dentry)),
1814                                               new_dentry, key);
1815                         if (ret)
1816                                 goto error_rehash;
1817
1818                         new_dentry = tmp;
1819                         rehash = NULL;
1820                         new_negative = true;
1821                 }
1822         }
1823
1824         /* This bit is potentially nasty as there's a potential race with
1825          * afs_d_revalidate{,_rcu}().  We have to change d_fsdata on the dentry
1826          * to reflect it's new parent's new data_version after the op, but
1827          * d_revalidate may see old_dentry between the op having taken place
1828          * and the version being updated.
1829          *
1830          * So drop the old_dentry for now to make other threads go through
1831          * lookup instead - which we hold a lock against.
1832          */
1833         d_drop(old_dentry);
1834
1835         ret = -ERESTARTSYS;
1836         if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1837                 afs_dataversion_t orig_data_version;
1838                 afs_dataversion_t new_data_version;
1839                 struct afs_status_cb *new_scb = &scb[1];
1840
1841                 orig_data_version = orig_dvnode->status.data_version + 1;
1842
1843                 if (orig_dvnode != new_dvnode) {
1844                         if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1845                                 afs_end_vnode_operation(&fc);
1846                                 goto error_rehash_old;
1847                         }
1848                         new_data_version = new_dvnode->status.data_version + 1;
1849                 } else {
1850                         new_data_version = orig_data_version;
1851                         new_scb = &scb[0];
1852                 }
1853
1854                 while (afs_select_fileserver(&fc)) {
1855                         fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1856                         fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1857                         afs_fs_rename(&fc, old_dentry->d_name.name,
1858                                       new_dvnode, new_dentry->d_name.name,
1859                                       &scb[0], new_scb);
1860                 }
1861
1862                 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1863                                         &orig_data_version, &scb[0]);
1864                 if (new_dvnode != orig_dvnode) {
1865                         afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1866                                                 &new_data_version, &scb[1]);
1867                         mutex_unlock(&new_dvnode->io_lock);
1868                 }
1869                 ret = afs_end_vnode_operation(&fc);
1870                 if (ret < 0)
1871                         goto error_rehash_old;
1872         }
1873
1874         if (ret == 0) {
1875                 if (rehash)
1876                         d_rehash(rehash);
1877                 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1878                     afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1879                                         afs_edit_dir_for_rename_0);
1880
1881                 if (!new_negative &&
1882                     test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1883                         afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1884                                             afs_edit_dir_for_rename_1);
1885
1886                 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1887                         afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1888                                          &vnode->fid, afs_edit_dir_for_rename_2);
1889
1890                 new_inode = d_inode(new_dentry);
1891                 if (new_inode) {
1892                         spin_lock(&new_inode->i_lock);
1893                         if (new_inode->i_nlink > 0)
1894                                 drop_nlink(new_inode);
1895                         spin_unlock(&new_inode->i_lock);
1896                 }
1897
1898                 /* Now we can update d_fsdata on the dentries to reflect their
1899                  * new parent's data_version.
1900                  *
1901                  * Note that if we ever implement RENAME_EXCHANGE, we'll have
1902                  * to update both dentries with opposing dir versions.
1903                  */
1904                 if (new_dvnode != orig_dvnode) {
1905                         afs_update_dentry_version(&fc, old_dentry, &scb[1]);
1906                         afs_update_dentry_version(&fc, new_dentry, &scb[1]);
1907                 } else {
1908                         afs_update_dentry_version(&fc, old_dentry, &scb[0]);
1909                         afs_update_dentry_version(&fc, new_dentry, &scb[0]);
1910                 }
1911                 d_move(old_dentry, new_dentry);
1912                 goto error_tmp;
1913         }
1914
1915 error_rehash_old:
1916         d_rehash(new_dentry);
1917 error_rehash:
1918         if (rehash)
1919                 d_rehash(rehash);
1920 error_tmp:
1921         if (tmp)
1922                 dput(tmp);
1923         key_put(key);
1924 error_scb:
1925         kfree(scb);
1926 error:
1927         _leave(" = %d", ret);
1928         return ret;
1929 }
1930
1931 /*
1932  * Release a directory page and clean up its private state if it's not busy
1933  * - return true if the page can now be released, false if not
1934  */
1935 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1936 {
1937         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1938
1939         _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1940
1941         set_page_private(page, 0);
1942         ClearPagePrivate(page);
1943
1944         /* The directory will need reloading. */
1945         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1946                 afs_stat_v(dvnode, n_relpg);
1947         return 1;
1948 }
1949
1950 /*
1951  * invalidate part or all of a page
1952  * - release a page and clean up its private data if offset is 0 (indicating
1953  *   the entire page)
1954  */
1955 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1956                                    unsigned int length)
1957 {
1958         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1959
1960         _enter("{%lu},%u,%u", page->index, offset, length);
1961
1962         BUG_ON(!PageLocked(page));
1963
1964         /* The directory will need reloading. */
1965         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1966                 afs_stat_v(dvnode, n_inval);
1967
1968         /* we clean up only if the entire page is being invalidated */
1969         if (offset == 0 && length == PAGE_SIZE) {
1970                 set_page_private(page, 0);
1971                 ClearPagePrivate(page);
1972         }
1973 }