blob: 719af4fb15dc5dfbb42322f953ba7012dd763a0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
David Howells260a9802007-04-26 15:59:35 -070021static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
22 struct nameidata *nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static int afs_dir_open(struct inode *inode, struct file *file);
David Howells260a9802007-04-26 15:59:35 -070024static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
26static int afs_d_delete(struct dentry *dentry);
David Howells260a9802007-04-26 15:59:35 -070027static void afs_d_release(struct dentry *dentry);
28static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -070029 loff_t fpos, u64 ino, unsigned dtype);
David Howells260a9802007-04-26 15:59:35 -070030static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
31 struct nameidata *nd);
32static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
33static int afs_rmdir(struct inode *dir, struct dentry *dentry);
34static int afs_unlink(struct inode *dir, struct dentry *dentry);
35static int afs_link(struct dentry *from, struct inode *dir,
36 struct dentry *dentry);
37static int afs_symlink(struct inode *dir, struct dentry *dentry,
38 const char *content);
39static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
40 struct inode *new_dir, struct dentry *new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070044 .release = afs_release,
David Howells260a9802007-04-26 15:59:35 -070045 .readdir = afs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Arjan van de Ven754661f2007-02-12 00:55:38 -080048const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070049 .create = afs_create,
50 .lookup = afs_lookup,
51 .link = afs_link,
52 .unlink = afs_unlink,
53 .symlink = afs_symlink,
54 .mkdir = afs_mkdir,
55 .rmdir = afs_rmdir,
56 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070057 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070058 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070059 .setattr = afs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62static struct dentry_operations afs_fs_dentry_operations = {
63 .d_revalidate = afs_d_revalidate,
64 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070065 .d_release = afs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68#define AFS_DIR_HASHTBL_SIZE 128
69#define AFS_DIR_DIRENT_SIZE 32
70#define AFS_DIRENT_PER_BLOCK 64
71
72union afs_dirent {
73 struct {
74 uint8_t valid;
75 uint8_t unused[1];
76 __be16 hash_next;
77 __be32 vnode;
78 __be32 unique;
79 uint8_t name[16];
80 uint8_t overflow[4]; /* if any char of the name (inc
81 * NUL) reaches here, consume
82 * the next dirent too */
83 } u;
84 uint8_t extended_name[32];
85};
86
87/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
88struct afs_dir_pagehdr {
89 __be16 npages;
90 __be16 magic;
91#define AFS_DIR_MAGIC htons(1234)
92 uint8_t nentries;
93 uint8_t bitmap[8];
94 uint8_t pad[19];
95};
96
97/* directory block layout */
98union afs_dir_block {
99
100 struct afs_dir_pagehdr pagehdr;
101
102 struct {
103 struct afs_dir_pagehdr pagehdr;
104 uint8_t alloc_ctrs[128];
105 /* dir hash table */
106 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
107 } hdr;
108
109 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
110};
111
112/* layout on a linux VM page */
113struct afs_dir_page {
114 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
115};
116
David Howells260a9802007-04-26 15:59:35 -0700117struct afs_lookup_cookie {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct afs_fid fid;
119 const char *name;
120 size_t nlen;
121 int found;
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
125 * check that a directory page is valid
126 */
127static inline void afs_dir_check_page(struct inode *dir, struct page *page)
128{
129 struct afs_dir_page *dbuf;
130 loff_t latter;
131 int tmp, qty;
132
133#if 0
134 /* check the page count */
135 qty = desc.size / sizeof(dbuf->blocks[0]);
136 if (qty == 0)
137 goto error;
138
David Howells08e0e7c2007-04-26 15:55:03 -0700139 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
David Howells08e0e7c2007-04-26 15:55:03 -0700141 __FUNCTION__, dir->i_ino, qty,
142 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto error;
144 }
145#endif
146
147 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800148 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (latter >= PAGE_SIZE)
150 qty = PAGE_SIZE;
151 else
152 qty = latter;
153 qty /= sizeof(union afs_dir_block);
154
155 /* check them */
156 dbuf = page_address(page);
157 for (tmp = 0; tmp < qty; tmp++) {
158 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
159 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
160 __FUNCTION__, dir->i_ino, tmp, qty,
161 ntohs(dbuf->blocks[tmp].pagehdr.magic));
162 goto error;
163 }
164 }
165
166 SetPageChecked(page);
167 return;
168
David Howellsec268152007-04-26 15:49:28 -0700169error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 SetPageChecked(page);
171 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * discard a page cached in the pagecache
176 */
177static inline void afs_dir_put_page(struct page *page)
178{
179 kunmap(page);
180 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * get a page into the pagecache
185 */
David Howells00d3b7a2007-04-26 15:57:07 -0700186static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
187 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct page *page;
David Howells00d3b7a2007-04-26 15:57:07 -0700190 struct file file = {
191 .private_data = key,
192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 _enter("{%lu},%lu", dir->i_ino, index);
195
David Howells00d3b7a2007-04-26 15:57:07 -0700196 page = read_mapping_page(dir->i_mapping, index, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!PageChecked(page))
200 afs_dir_check_page(dir, page);
201 if (PageError(page))
202 goto fail;
203 }
204 return page;
205
David Howellsec268152007-04-26 15:49:28 -0700206fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700208 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * open an AFS directory file
214 */
215static int afs_dir_open(struct inode *inode, struct file *file)
216{
217 _enter("{%lu}", inode->i_ino);
218
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700219 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
220 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
David Howells08e0e7c2007-04-26 15:55:03 -0700222 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -ENOENT;
224
David Howells00d3b7a2007-04-26 15:57:07 -0700225 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * deal with one block in an AFS directory
230 */
231static int afs_dir_iterate_block(unsigned *fpos,
232 union afs_dir_block *block,
233 unsigned blkoff,
234 void *cookie,
235 filldir_t filldir)
236{
237 union afs_dirent *dire;
238 unsigned offset, next, curr;
239 size_t nlen;
240 int tmp, ret;
241
242 _enter("%u,%x,%p,,",*fpos,blkoff,block);
243
244 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
245
246 /* walk through the block, an entry at a time */
247 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
248 offset < AFS_DIRENT_PER_BLOCK;
249 offset = next
250 ) {
251 next = offset + 1;
252
253 /* skip entries marked unused in the bitmap */
254 if (!(block->pagehdr.bitmap[offset / 8] &
255 (1 << (offset % 8)))) {
David Howells08e0e7c2007-04-26 15:55:03 -0700256 _debug("ENT[%Zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 blkoff / sizeof(union afs_dir_block), offset);
258 if (offset >= curr)
259 *fpos = blkoff +
260 next * sizeof(union afs_dirent);
261 continue;
262 }
263
264 /* got a valid entry */
265 dire = &block->dirents[offset];
266 nlen = strnlen(dire->u.name,
267 sizeof(*block) -
268 offset * sizeof(union afs_dirent));
269
David Howells08e0e7c2007-04-26 15:55:03 -0700270 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 blkoff / sizeof(union afs_dir_block), offset,
272 (offset < curr ? "skip" : "fill"),
273 nlen, dire->u.name);
274
275 /* work out where the next possible entry is */
276 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
277 if (next >= AFS_DIRENT_PER_BLOCK) {
278 _debug("ENT[%Zu.%u]:"
279 " %u travelled beyond end dir block"
David Howells08e0e7c2007-04-26 15:55:03 -0700280 " (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 blkoff / sizeof(union afs_dir_block),
282 offset, next, tmp, nlen);
283 return -EIO;
284 }
285 if (!(block->pagehdr.bitmap[next / 8] &
286 (1 << (next % 8)))) {
287 _debug("ENT[%Zu.%u]:"
David Howells08e0e7c2007-04-26 15:55:03 -0700288 " %u unmarked extension (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 blkoff / sizeof(union afs_dir_block),
290 offset, next, tmp, nlen);
291 return -EIO;
292 }
293
David Howells08e0e7c2007-04-26 15:55:03 -0700294 _debug("ENT[%Zu.%u]: ext %u/%Zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 blkoff / sizeof(union afs_dir_block),
296 next, tmp, nlen);
297 next++;
298 }
299
300 /* skip if starts before the current position */
301 if (offset < curr)
302 continue;
303
304 /* found the next entry */
305 ret = filldir(cookie,
306 dire->u.name,
307 nlen,
308 blkoff + offset * sizeof(union afs_dirent),
309 ntohl(dire->u.vnode),
David Howells260a9802007-04-26 15:59:35 -0700310 filldir == afs_lookup_filldir ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ntohl(dire->u.unique) : DT_UNKNOWN);
312 if (ret < 0) {
313 _leave(" = 0 [full]");
314 return 0;
315 }
316
317 *fpos = blkoff + next * sizeof(union afs_dirent);
318 }
319
320 _leave(" = 1 [more]");
321 return 1;
David Howellsec268152007-04-26 15:49:28 -0700322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
David Howells08e0e7c2007-04-26 15:55:03 -0700325 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
David Howells00d3b7a2007-04-26 15:57:07 -0700328 filldir_t filldir, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
David Howells08e0e7c2007-04-26 15:55:03 -0700330 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct afs_dir_page *dbuf;
332 struct page *page;
333 unsigned blkoff, limit;
334 int ret;
335
336 _enter("{%lu},%u,,", dir->i_ino, *fpos);
337
David Howells08e0e7c2007-04-26 15:55:03 -0700338 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 _leave(" = -ESTALE");
340 return -ESTALE;
341 }
342
343 /* round the file position up to the next entry boundary */
344 *fpos += sizeof(union afs_dirent) - 1;
345 *fpos &= ~(sizeof(union afs_dirent) - 1);
346
347 /* walk through the blocks in sequence */
348 ret = 0;
349 while (*fpos < dir->i_size) {
350 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
351
352 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700353 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (IS_ERR(page)) {
355 ret = PTR_ERR(page);
356 break;
357 }
358
359 limit = blkoff & ~(PAGE_SIZE - 1);
360
361 dbuf = page_address(page);
362
363 /* deal with the individual blocks stashed on this page */
364 do {
365 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
366 sizeof(union afs_dir_block)];
367 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
368 cookie, filldir);
369 if (ret != 1) {
370 afs_dir_put_page(page);
371 goto out;
372 }
373
374 blkoff += sizeof(union afs_dir_block);
375
376 } while (*fpos < dir->i_size && blkoff < limit);
377
378 afs_dir_put_page(page);
379 ret = 0;
380 }
381
David Howellsec268152007-04-26 15:49:28 -0700382out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 _leave(" = %d", ret);
384 return ret;
David Howellsec268152007-04-26 15:49:28 -0700385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * read an AFS directory
389 */
David Howells260a9802007-04-26 15:59:35 -0700390static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned fpos;
393 int ret;
394
David Howells08e0e7c2007-04-26 15:55:03 -0700395 _enter("{%Ld,{%lu}}",
396 file->f_pos, file->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Howells00d3b7a2007-04-26 15:57:07 -0700398 ASSERT(file->private_data != NULL);
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 fpos = file->f_pos;
David Howells08e0e7c2007-04-26 15:55:03 -0700401 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
David Howells00d3b7a2007-04-26 15:57:07 -0700402 cookie, filldir, file->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 file->f_pos = fpos;
404
405 _leave(" = %d", ret);
406 return ret;
David Howellsec268152007-04-26 15:49:28 -0700407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * search the directory for a name
411 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
412 * uniquifier through dtype
413 */
David Howells260a9802007-04-26 15:59:35 -0700414static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
415 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
David Howells260a9802007-04-26 15:59:35 -0700417 struct afs_lookup_cookie *cookie = _cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Howells08e0e7c2007-04-26 15:55:03 -0700419 _enter("{%s,%Zu},%s,%u,,%llu,%u",
David S. Millerba3e0e12007-04-26 16:06:22 -0700420 cookie->name, cookie->nlen, name, nlen,
421 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David Howells08e0e7c2007-04-26 15:55:03 -0700423 /* insanity checks first */
424 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
425 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
428 _leave(" = 0 [no]");
429 return 0;
430 }
431
432 cookie->fid.vnode = ino;
433 cookie->fid.unique = dtype;
434 cookie->found = 1;
435
436 _leave(" = -1 [found]");
437 return -1;
David Howellsec268152007-04-26 15:49:28 -0700438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
David Howells08e0e7c2007-04-26 15:55:03 -0700441 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700442 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
David Howells08e0e7c2007-04-26 15:55:03 -0700444static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700445 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
David Howells260a9802007-04-26 15:59:35 -0700447 struct afs_lookup_cookie cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unsigned fpos;
450 int ret;
451
David Howells08e0e7c2007-04-26 15:55:03 -0700452 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 as = dir->i_sb->s_fs_info;
455
456 /* search the directory */
457 cookie.name = dentry->d_name.name;
458 cookie.nlen = dentry->d_name.len;
459 cookie.fid.vid = as->volume->vid;
460 cookie.found = 0;
461
462 fpos = 0;
David Howells260a9802007-04-26 15:59:35 -0700463 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
David Howells00d3b7a2007-04-26 15:57:07 -0700464 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700466 _leave(" = %d [iter]", ret);
467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 ret = -ENOENT;
471 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700472 _leave(" = -ENOENT [not found]");
473 return -ENOENT;
474 }
475
476 *fid = cookie.fid;
477 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
478 return 0;
479}
480
481/*
482 * look up an entry in a directory
483 */
David Howells260a9802007-04-26 15:59:35 -0700484static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
485 struct nameidata *nd)
David Howells08e0e7c2007-04-26 15:55:03 -0700486{
487 struct afs_vnode *vnode;
488 struct afs_fid fid;
489 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700490 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700491 int ret;
492
David Howells260a9802007-04-26 15:59:35 -0700493 vnode = AFS_FS_I(dir);
494
David Howells416351f2007-05-09 02:33:45 -0700495 _enter("{%x:%u},%p{%s},",
David Howells260a9802007-04-26 15:59:35 -0700496 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
497
498 ASSERTCMP(dentry->d_inode, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700499
David Howells45222b92007-05-10 22:22:20 -0700500 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700501 _leave(" = -ENAMETOOLONG");
502 return ERR_PTR(-ENAMETOOLONG);
503 }
504
David Howells08e0e7c2007-04-26 15:55:03 -0700505 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
506 _leave(" = -ESTALE");
507 return ERR_PTR(-ESTALE);
508 }
509
David Howells00d3b7a2007-04-26 15:57:07 -0700510 key = afs_request_key(vnode->volume->cell);
511 if (IS_ERR(key)) {
512 _leave(" = %ld [key]", PTR_ERR(key));
513 return ERR_PTR(PTR_ERR(key));
514 }
515
David Howells260a9802007-04-26 15:59:35 -0700516 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700517 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700518 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700519 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return ERR_PTR(ret);
521 }
522
David Howells260a9802007-04-26 15:59:35 -0700523 ret = afs_do_lookup(dir, dentry, &fid, key);
524 if (ret < 0) {
525 key_put(key);
526 if (ret == -ENOENT) {
527 d_add(dentry, NULL);
528 _leave(" = NULL [negative]");
529 return NULL;
530 }
531 _leave(" = %d [do]", ret);
532 return ERR_PTR(ret);
533 }
534 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700537 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700538 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700539 if (IS_ERR(inode)) {
540 _leave(" = %ld", PTR_ERR(inode));
541 return ERR_PTR(PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 dentry->d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 d_add(dentry, inode);
547 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
David Howells08e0e7c2007-04-26 15:55:03 -0700548 fid.vnode,
549 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 dentry->d_inode->i_ino,
551 dentry->d_inode->i_version);
552
553 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * check that a dentry lookup hit has found a valid entry
558 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
559 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
562{
David Howells260a9802007-04-26 15:59:35 -0700563 struct afs_vnode *vnode, *dir;
David Howells08e0e7c2007-04-26 15:55:03 -0700564 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700566 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700567 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int ret;
569
David Howells08e0e7c2007-04-26 15:55:03 -0700570 vnode = AFS_FS_I(dentry->d_inode);
571
David Howells260a9802007-04-26 15:59:35 -0700572 if (dentry->d_inode)
573 _enter("{v={%x:%u} n=%s fl=%lx},",
574 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
575 vnode->flags);
576 else
577 _enter("{neg n=%s}", dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Howells260a9802007-04-26 15:59:35 -0700579 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700580 if (IS_ERR(key))
581 key = NULL;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700584 parent = dget_parent(dentry);
David Howells260a9802007-04-26 15:59:35 -0700585 if (!parent->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto out_bad;
587
David Howells260a9802007-04-26 15:59:35 -0700588 dir = AFS_FS_I(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
David Howells260a9802007-04-26 15:59:35 -0700590 /* validate the parent directory */
591 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
592 afs_validate(dir, key);
593
594 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 _debug("%s: parent dir deleted", dentry->d_name.name);
596 goto out_bad;
597 }
598
David Howells260a9802007-04-26 15:59:35 -0700599 dir_version = (void *) (unsigned long) dir->status.data_version;
600 if (dentry->d_fsdata == dir_version)
601 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
David Howells260a9802007-04-26 15:59:35 -0700603 _debug("dir modified");
604
605 /* search the directory for this vnode */
606 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
607 switch (ret) {
608 case 0:
609 /* the filename maps to something */
610 if (!dentry->d_inode)
611 goto out_bad;
612 if (is_bad_inode(dentry->d_inode)) {
613 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
614 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto out_bad;
616 }
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* if the vnode ID has changed, then the dirent points to a
619 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700620 if (fid.vnode != vnode->fid.vnode) {
621 _debug("%s: dirent changed [%u != %u]",
622 dentry->d_name.name, fid.vnode,
623 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto not_found;
625 }
626
627 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700628 * been deleted and replaced, and the original vnode ID has
629 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700630 if (fid.unique != vnode->fid.unique) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 _debug("%s: file deleted (uq %u -> %u I:%lu)",
David Howells08e0e7c2007-04-26 15:55:03 -0700632 dentry->d_name.name, fid.unique,
David Howells260a9802007-04-26 15:59:35 -0700633 vnode->fid.unique, dentry->d_inode->i_version);
David Howells08e0e7c2007-04-26 15:55:03 -0700634 spin_lock(&vnode->lock);
635 set_bit(AFS_VNODE_DELETED, &vnode->flags);
636 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700637 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
David Howells260a9802007-04-26 15:59:35 -0700639 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
David Howells260a9802007-04-26 15:59:35 -0700641 case -ENOENT:
642 /* the filename is unknown */
643 _debug("%s: dirent not found", dentry->d_name.name);
644 if (dentry->d_inode)
645 goto not_found;
646 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700647
David Howells260a9802007-04-26 15:59:35 -0700648 default:
649 _debug("failed to iterate dir %s: %d",
650 parent->d_name.name, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700651 goto out_bad;
652 }
653
David Howellsec268152007-04-26 15:49:28 -0700654out_valid:
David Howells260a9802007-04-26 15:59:35 -0700655 dentry->d_fsdata = dir_version;
656out_skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700658 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 _leave(" = 1 [valid]");
660 return 1;
661
662 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700663not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_lock(&dentry->d_lock);
665 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
666 spin_unlock(&dentry->d_lock);
667
David Howellsec268152007-04-26 15:49:28 -0700668out_bad:
David Howells260a9802007-04-26 15:59:35 -0700669 if (dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* don't unhash if we have submounts */
671 if (have_submounts(dentry))
David Howells260a9802007-04-26 15:59:35 -0700672 goto out_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700676 parent->d_name.name, dentry->d_name.name);
677 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700680 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 _leave(" = 0 [bad]");
683 return 0;
David Howellsec268152007-04-26 15:49:28 -0700684}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
687 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
688 * sleep)
689 * - called from dput() when d_count is going to 0.
690 * - return 1 to request dentry be unhashed, 0 otherwise
691 */
692static int afs_d_delete(struct dentry *dentry)
693{
694 _enter("%s", dentry->d_name.name);
695
696 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
697 goto zap;
698
David Howells08e0e7c2007-04-26 15:55:03 -0700699 if (dentry->d_inode &&
700 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 _leave(" = 0 [keep]");
704 return 0;
705
David Howellsec268152007-04-26 15:49:28 -0700706zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 _leave(" = 1 [zap]");
708 return 1;
David Howellsec268152007-04-26 15:49:28 -0700709}
David Howells260a9802007-04-26 15:59:35 -0700710
711/*
712 * handle dentry release
713 */
714static void afs_d_release(struct dentry *dentry)
715{
716 _enter("%s", dentry->d_name.name);
717}
718
719/*
720 * create a directory on an AFS filesystem
721 */
722static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
723{
724 struct afs_file_status status;
725 struct afs_callback cb;
726 struct afs_server *server;
727 struct afs_vnode *dvnode, *vnode;
728 struct afs_fid fid;
729 struct inode *inode;
730 struct key *key;
731 int ret;
732
733 dvnode = AFS_FS_I(dir);
734
David Howells416351f2007-05-09 02:33:45 -0700735 _enter("{%x:%u},{%s},%o",
David Howells260a9802007-04-26 15:59:35 -0700736 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
737
738 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700739 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700740 goto error;
741
742 key = afs_request_key(dvnode->volume->cell);
743 if (IS_ERR(key)) {
744 ret = PTR_ERR(key);
745 goto error;
746 }
747
748 mode |= S_IFDIR;
749 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
750 mode, &fid, &status, &cb, &server);
751 if (ret < 0)
752 goto mkdir_error;
753
754 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
755 if (IS_ERR(inode)) {
756 /* ENOMEM at a really inconvenient time - just abandon the new
757 * directory on the server */
758 ret = PTR_ERR(inode);
759 goto iget_error;
760 }
761
762 /* apply the status report we've got for the new vnode */
763 vnode = AFS_FS_I(inode);
764 spin_lock(&vnode->lock);
765 vnode->update_cnt++;
766 spin_unlock(&vnode->lock);
767 afs_vnode_finalise_status_update(vnode, server);
768 afs_put_server(server);
769
770 d_instantiate(dentry, inode);
771 if (d_unhashed(dentry)) {
772 _debug("not hashed");
773 d_rehash(dentry);
774 }
775 key_put(key);
776 _leave(" = 0");
777 return 0;
778
779iget_error:
780 afs_put_server(server);
781mkdir_error:
782 key_put(key);
783error:
784 d_drop(dentry);
785 _leave(" = %d", ret);
786 return ret;
787}
788
789/*
790 * remove a directory from an AFS filesystem
791 */
792static int afs_rmdir(struct inode *dir, struct dentry *dentry)
793{
794 struct afs_vnode *dvnode, *vnode;
795 struct key *key;
796 int ret;
797
798 dvnode = AFS_FS_I(dir);
799
David Howells416351f2007-05-09 02:33:45 -0700800 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700801 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
802
803 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700804 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700805 goto error;
806
807 key = afs_request_key(dvnode->volume->cell);
808 if (IS_ERR(key)) {
809 ret = PTR_ERR(key);
810 goto error;
811 }
812
813 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
814 if (ret < 0)
815 goto rmdir_error;
816
817 if (dentry->d_inode) {
818 vnode = AFS_FS_I(dentry->d_inode);
819 clear_nlink(&vnode->vfs_inode);
820 set_bit(AFS_VNODE_DELETED, &vnode->flags);
821 afs_discard_callback_on_delete(vnode);
822 }
823
824 key_put(key);
825 _leave(" = 0");
826 return 0;
827
828rmdir_error:
829 key_put(key);
830error:
831 _leave(" = %d", ret);
832 return ret;
833}
834
835/*
836 * remove a file from an AFS filesystem
837 */
838static int afs_unlink(struct inode *dir, struct dentry *dentry)
839{
840 struct afs_vnode *dvnode, *vnode;
841 struct key *key;
842 int ret;
843
844 dvnode = AFS_FS_I(dir);
845
David Howells416351f2007-05-09 02:33:45 -0700846 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700847 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
848
849 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700850 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700851 goto error;
852
853 key = afs_request_key(dvnode->volume->cell);
854 if (IS_ERR(key)) {
855 ret = PTR_ERR(key);
856 goto error;
857 }
858
859 if (dentry->d_inode) {
860 vnode = AFS_FS_I(dentry->d_inode);
861
862 /* make sure we have a callback promise on the victim */
863 ret = afs_validate(vnode, key);
864 if (ret < 0)
865 goto error;
866 }
867
868 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
869 if (ret < 0)
870 goto remove_error;
871
872 if (dentry->d_inode) {
873 /* if the file wasn't deleted due to excess hard links, the
874 * fileserver will break the callback promise on the file - if
875 * it had one - before it returns to us, and if it was deleted,
876 * it won't
877 *
878 * however, if we didn't have a callback promise outstanding,
879 * or it was outstanding on a different server, then it won't
880 * break it either...
881 */
882 vnode = AFS_FS_I(dentry->d_inode);
883 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
884 _debug("AFS_VNODE_DELETED");
885 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
886 _debug("AFS_VNODE_CB_BROKEN");
887 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
888 ret = afs_validate(vnode, key);
889 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
890 }
891
892 key_put(key);
893 _leave(" = 0");
894 return 0;
895
896remove_error:
897 key_put(key);
898error:
899 _leave(" = %d", ret);
900 return ret;
901}
902
903/*
904 * create a regular file on an AFS filesystem
905 */
906static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
907 struct nameidata *nd)
908{
909 struct afs_file_status status;
910 struct afs_callback cb;
911 struct afs_server *server;
912 struct afs_vnode *dvnode, *vnode;
913 struct afs_fid fid;
914 struct inode *inode;
915 struct key *key;
916 int ret;
917
918 dvnode = AFS_FS_I(dir);
919
David Howells416351f2007-05-09 02:33:45 -0700920 _enter("{%x:%u},{%s},%o,",
David Howells260a9802007-04-26 15:59:35 -0700921 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
922
923 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700924 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700925 goto error;
926
927 key = afs_request_key(dvnode->volume->cell);
928 if (IS_ERR(key)) {
929 ret = PTR_ERR(key);
930 goto error;
931 }
932
933 mode |= S_IFREG;
934 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
935 mode, &fid, &status, &cb, &server);
936 if (ret < 0)
937 goto create_error;
938
939 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
940 if (IS_ERR(inode)) {
941 /* ENOMEM at a really inconvenient time - just abandon the new
942 * directory on the server */
943 ret = PTR_ERR(inode);
944 goto iget_error;
945 }
946
947 /* apply the status report we've got for the new vnode */
948 vnode = AFS_FS_I(inode);
949 spin_lock(&vnode->lock);
950 vnode->update_cnt++;
951 spin_unlock(&vnode->lock);
952 afs_vnode_finalise_status_update(vnode, server);
953 afs_put_server(server);
954
955 d_instantiate(dentry, inode);
956 if (d_unhashed(dentry)) {
957 _debug("not hashed");
958 d_rehash(dentry);
959 }
960 key_put(key);
961 _leave(" = 0");
962 return 0;
963
964iget_error:
965 afs_put_server(server);
966create_error:
967 key_put(key);
968error:
969 d_drop(dentry);
970 _leave(" = %d", ret);
971 return ret;
972}
973
974/*
975 * create a hard link between files in an AFS filesystem
976 */
977static int afs_link(struct dentry *from, struct inode *dir,
978 struct dentry *dentry)
979{
980 struct afs_vnode *dvnode, *vnode;
981 struct key *key;
982 int ret;
983
984 vnode = AFS_FS_I(from->d_inode);
985 dvnode = AFS_FS_I(dir);
986
David Howells416351f2007-05-09 02:33:45 -0700987 _enter("{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700988 vnode->fid.vid, vnode->fid.vnode,
989 dvnode->fid.vid, dvnode->fid.vnode,
990 dentry->d_name.name);
991
992 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700993 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700994 goto error;
995
996 key = afs_request_key(dvnode->volume->cell);
997 if (IS_ERR(key)) {
998 ret = PTR_ERR(key);
999 goto error;
1000 }
1001
1002 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1003 if (ret < 0)
1004 goto link_error;
1005
1006 atomic_inc(&vnode->vfs_inode.i_count);
1007 d_instantiate(dentry, &vnode->vfs_inode);
1008 key_put(key);
1009 _leave(" = 0");
1010 return 0;
1011
1012link_error:
1013 key_put(key);
1014error:
1015 d_drop(dentry);
1016 _leave(" = %d", ret);
1017 return ret;
1018}
1019
1020/*
1021 * create a symlink in an AFS filesystem
1022 */
1023static int afs_symlink(struct inode *dir, struct dentry *dentry,
1024 const char *content)
1025{
1026 struct afs_file_status status;
1027 struct afs_server *server;
1028 struct afs_vnode *dvnode, *vnode;
1029 struct afs_fid fid;
1030 struct inode *inode;
1031 struct key *key;
1032 int ret;
1033
1034 dvnode = AFS_FS_I(dir);
1035
David Howells416351f2007-05-09 02:33:45 -07001036 _enter("{%x:%u},{%s},%s",
David Howells260a9802007-04-26 15:59:35 -07001037 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1038 content);
1039
1040 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001041 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001042 goto error;
1043
1044 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001045 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001046 goto error;
1047
1048 key = afs_request_key(dvnode->volume->cell);
1049 if (IS_ERR(key)) {
1050 ret = PTR_ERR(key);
1051 goto error;
1052 }
1053
1054 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1055 &fid, &status, &server);
1056 if (ret < 0)
1057 goto create_error;
1058
1059 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1060 if (IS_ERR(inode)) {
1061 /* ENOMEM at a really inconvenient time - just abandon the new
1062 * directory on the server */
1063 ret = PTR_ERR(inode);
1064 goto iget_error;
1065 }
1066
1067 /* apply the status report we've got for the new vnode */
1068 vnode = AFS_FS_I(inode);
1069 spin_lock(&vnode->lock);
1070 vnode->update_cnt++;
1071 spin_unlock(&vnode->lock);
1072 afs_vnode_finalise_status_update(vnode, server);
1073 afs_put_server(server);
1074
1075 d_instantiate(dentry, inode);
1076 if (d_unhashed(dentry)) {
1077 _debug("not hashed");
1078 d_rehash(dentry);
1079 }
1080 key_put(key);
1081 _leave(" = 0");
1082 return 0;
1083
1084iget_error:
1085 afs_put_server(server);
1086create_error:
1087 key_put(key);
1088error:
1089 d_drop(dentry);
1090 _leave(" = %d", ret);
1091 return ret;
1092}
1093
1094/*
1095 * rename a file in an AFS filesystem and/or move it between directories
1096 */
1097static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1098 struct inode *new_dir, struct dentry *new_dentry)
1099{
1100 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1101 struct key *key;
1102 int ret;
1103
1104 vnode = AFS_FS_I(old_dentry->d_inode);
1105 orig_dvnode = AFS_FS_I(old_dir);
1106 new_dvnode = AFS_FS_I(new_dir);
1107
David Howells416351f2007-05-09 02:33:45 -07001108 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001109 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1110 vnode->fid.vid, vnode->fid.vnode,
1111 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1112 new_dentry->d_name.name);
1113
1114 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001115 if (new_dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001116 goto error;
1117
1118 key = afs_request_key(orig_dvnode->volume->cell);
1119 if (IS_ERR(key)) {
1120 ret = PTR_ERR(key);
1121 goto error;
1122 }
1123
1124 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1125 old_dentry->d_name.name,
1126 new_dentry->d_name.name);
1127 if (ret < 0)
1128 goto rename_error;
1129 key_put(key);
1130 _leave(" = 0");
1131 return 0;
1132
1133rename_error:
1134 key_put(key);
1135error:
1136 d_drop(new_dentry);
1137 _leave(" = %d", ret);
1138 return ret;
1139}