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