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