blob: b42d5cc1d6d21aace96eaae96fe3aa521822c7c9 [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/fs.h>
16#include <linux/pagemap.h>
David Howells00d3b7a2007-04-26 15:57:07 -070017#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.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,
David Howellse8d6c552007-07-15 23:40:12 -070046 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020047 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Arjan van de Ven754661f2007-02-12 00:55:38 -080050const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070051 .create = afs_create,
52 .lookup = afs_lookup,
53 .link = afs_link,
54 .unlink = afs_unlink,
55 .symlink = afs_symlink,
56 .mkdir = afs_mkdir,
57 .rmdir = afs_rmdir,
58 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070059 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070060 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070061 .setattr = afs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Al Viro79be57c2009-02-20 05:56:47 +000064static const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .d_revalidate = afs_d_revalidate,
66 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070067 .d_release = afs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70#define AFS_DIR_HASHTBL_SIZE 128
71#define AFS_DIR_DIRENT_SIZE 32
72#define AFS_DIRENT_PER_BLOCK 64
73
74union afs_dirent {
75 struct {
76 uint8_t valid;
77 uint8_t unused[1];
78 __be16 hash_next;
79 __be32 vnode;
80 __be32 unique;
81 uint8_t name[16];
82 uint8_t overflow[4]; /* if any char of the name (inc
83 * NUL) reaches here, consume
84 * the next dirent too */
85 } u;
86 uint8_t extended_name[32];
87};
88
89/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
90struct afs_dir_pagehdr {
91 __be16 npages;
92 __be16 magic;
93#define AFS_DIR_MAGIC htons(1234)
94 uint8_t nentries;
95 uint8_t bitmap[8];
96 uint8_t pad[19];
97};
98
99/* directory block layout */
100union afs_dir_block {
101
102 struct afs_dir_pagehdr pagehdr;
103
104 struct {
105 struct afs_dir_pagehdr pagehdr;
106 uint8_t alloc_ctrs[128];
107 /* dir hash table */
108 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
109 } hdr;
110
111 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
112};
113
114/* layout on a linux VM page */
115struct afs_dir_page {
116 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
117};
118
David Howells260a9802007-04-26 15:59:35 -0700119struct afs_lookup_cookie {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct afs_fid fid;
121 const char *name;
122 size_t nlen;
123 int found;
124};
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 * check that a directory page is valid
128 */
129static inline void afs_dir_check_page(struct inode *dir, struct page *page)
130{
131 struct afs_dir_page *dbuf;
132 loff_t latter;
133 int tmp, qty;
134
135#if 0
136 /* check the page count */
137 qty = desc.size / sizeof(dbuf->blocks[0]);
138 if (qty == 0)
139 goto error;
140
David Howells08e0e7c2007-04-26 15:55:03 -0700141 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700143 __func__, dir->i_ino, qty,
David Howells08e0e7c2007-04-26 15:55:03 -0700144 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto error;
146 }
147#endif
148
149 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800150 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (latter >= PAGE_SIZE)
152 qty = PAGE_SIZE;
153 else
154 qty = latter;
155 qty /= sizeof(union afs_dir_block);
156
157 /* check them */
158 dbuf = page_address(page);
159 for (tmp = 0; tmp < qty; tmp++) {
160 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
161 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700162 __func__, dir->i_ino, tmp, qty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ntohs(dbuf->blocks[tmp].pagehdr.magic));
164 goto error;
165 }
166 }
167
168 SetPageChecked(page);
169 return;
170
David Howellsec268152007-04-26 15:49:28 -0700171error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 SetPageChecked(page);
173 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
177 * discard a page cached in the pagecache
178 */
179static inline void afs_dir_put_page(struct page *page)
180{
181 kunmap(page);
182 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * get a page into the pagecache
187 */
David Howells00d3b7a2007-04-26 15:57:07 -0700188static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
189 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 _enter("{%lu},%lu", dir->i_ino, index);
193
Al Virof6d335c2010-05-21 15:27:09 +0100194 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!PageChecked(page))
198 afs_dir_check_page(dir, page);
199 if (PageError(page))
200 goto fail;
201 }
202 return page;
203
David Howellsec268152007-04-26 15:49:28 -0700204fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700206 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * open an AFS directory file
212 */
213static int afs_dir_open(struct inode *inode, struct file *file)
214{
215 _enter("{%lu}", inode->i_ino);
216
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700217 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
218 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
David Howells08e0e7c2007-04-26 15:55:03 -0700220 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -ENOENT;
222
David Howells00d3b7a2007-04-26 15:57:07 -0700223 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
227 * deal with one block in an AFS directory
228 */
229static int afs_dir_iterate_block(unsigned *fpos,
230 union afs_dir_block *block,
231 unsigned blkoff,
232 void *cookie,
233 filldir_t filldir)
234{
235 union afs_dirent *dire;
236 unsigned offset, next, curr;
237 size_t nlen;
238 int tmp, ret;
239
240 _enter("%u,%x,%p,,",*fpos,blkoff,block);
241
242 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
243
244 /* walk through the block, an entry at a time */
245 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
246 offset < AFS_DIRENT_PER_BLOCK;
247 offset = next
248 ) {
249 next = offset + 1;
250
251 /* skip entries marked unused in the bitmap */
252 if (!(block->pagehdr.bitmap[offset / 8] &
253 (1 << (offset % 8)))) {
David Howells08e0e7c2007-04-26 15:55:03 -0700254 _debug("ENT[%Zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 blkoff / sizeof(union afs_dir_block), offset);
256 if (offset >= curr)
257 *fpos = blkoff +
258 next * sizeof(union afs_dirent);
259 continue;
260 }
261
262 /* got a valid entry */
263 dire = &block->dirents[offset];
264 nlen = strnlen(dire->u.name,
265 sizeof(*block) -
266 offset * sizeof(union afs_dirent));
267
David Howells08e0e7c2007-04-26 15:55:03 -0700268 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 blkoff / sizeof(union afs_dir_block), offset,
270 (offset < curr ? "skip" : "fill"),
271 nlen, dire->u.name);
272
273 /* work out where the next possible entry is */
274 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
275 if (next >= AFS_DIRENT_PER_BLOCK) {
276 _debug("ENT[%Zu.%u]:"
277 " %u travelled beyond end dir block"
David Howells08e0e7c2007-04-26 15:55:03 -0700278 " (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 blkoff / sizeof(union afs_dir_block),
280 offset, next, tmp, nlen);
281 return -EIO;
282 }
283 if (!(block->pagehdr.bitmap[next / 8] &
284 (1 << (next % 8)))) {
285 _debug("ENT[%Zu.%u]:"
David Howells08e0e7c2007-04-26 15:55:03 -0700286 " %u unmarked extension (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 blkoff / sizeof(union afs_dir_block),
288 offset, next, tmp, nlen);
289 return -EIO;
290 }
291
David Howells08e0e7c2007-04-26 15:55:03 -0700292 _debug("ENT[%Zu.%u]: ext %u/%Zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 blkoff / sizeof(union afs_dir_block),
294 next, tmp, nlen);
295 next++;
296 }
297
298 /* skip if starts before the current position */
299 if (offset < curr)
300 continue;
301
302 /* found the next entry */
303 ret = filldir(cookie,
304 dire->u.name,
305 nlen,
306 blkoff + offset * sizeof(union afs_dirent),
307 ntohl(dire->u.vnode),
David Howells260a9802007-04-26 15:59:35 -0700308 filldir == afs_lookup_filldir ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 ntohl(dire->u.unique) : DT_UNKNOWN);
310 if (ret < 0) {
311 _leave(" = 0 [full]");
312 return 0;
313 }
314
315 *fpos = blkoff + next * sizeof(union afs_dirent);
316 }
317
318 _leave(" = 1 [more]");
319 return 1;
David Howellsec268152007-04-26 15:49:28 -0700320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
David Howells08e0e7c2007-04-26 15:55:03 -0700323 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
325static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
David Howells00d3b7a2007-04-26 15:57:07 -0700326 filldir_t filldir, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
David Howells08e0e7c2007-04-26 15:55:03 -0700328 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct afs_dir_page *dbuf;
330 struct page *page;
331 unsigned blkoff, limit;
332 int ret;
333
334 _enter("{%lu},%u,,", dir->i_ino, *fpos);
335
David Howells08e0e7c2007-04-26 15:55:03 -0700336 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 _leave(" = -ESTALE");
338 return -ESTALE;
339 }
340
341 /* round the file position up to the next entry boundary */
342 *fpos += sizeof(union afs_dirent) - 1;
343 *fpos &= ~(sizeof(union afs_dirent) - 1);
344
345 /* walk through the blocks in sequence */
346 ret = 0;
347 while (*fpos < dir->i_size) {
348 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
349
350 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700351 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (IS_ERR(page)) {
353 ret = PTR_ERR(page);
354 break;
355 }
356
357 limit = blkoff & ~(PAGE_SIZE - 1);
358
359 dbuf = page_address(page);
360
361 /* deal with the individual blocks stashed on this page */
362 do {
363 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
364 sizeof(union afs_dir_block)];
365 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
366 cookie, filldir);
367 if (ret != 1) {
368 afs_dir_put_page(page);
369 goto out;
370 }
371
372 blkoff += sizeof(union afs_dir_block);
373
374 } while (*fpos < dir->i_size && blkoff < limit);
375
376 afs_dir_put_page(page);
377 ret = 0;
378 }
379
David Howellsec268152007-04-26 15:49:28 -0700380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 _leave(" = %d", ret);
382 return ret;
David Howellsec268152007-04-26 15:49:28 -0700383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * read an AFS directory
387 */
David Howells260a9802007-04-26 15:59:35 -0700388static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 unsigned fpos;
391 int ret;
392
David Howells08e0e7c2007-04-26 15:55:03 -0700393 _enter("{%Ld,{%lu}}",
394 file->f_pos, file->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
David Howells00d3b7a2007-04-26 15:57:07 -0700396 ASSERT(file->private_data != NULL);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 fpos = file->f_pos;
David Howells08e0e7c2007-04-26 15:55:03 -0700399 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
David Howells00d3b7a2007-04-26 15:57:07 -0700400 cookie, filldir, file->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 file->f_pos = fpos;
402
403 _leave(" = %d", ret);
404 return ret;
David Howellsec268152007-04-26 15:49:28 -0700405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
408 * search the directory for a name
409 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
410 * uniquifier through dtype
411 */
David Howells260a9802007-04-26 15:59:35 -0700412static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
413 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
David Howells260a9802007-04-26 15:59:35 -0700415 struct afs_lookup_cookie *cookie = _cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells08e0e7c2007-04-26 15:55:03 -0700417 _enter("{%s,%Zu},%s,%u,,%llu,%u",
David S. Millerba3e0e12007-04-26 16:06:22 -0700418 cookie->name, cookie->nlen, name, nlen,
419 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David Howells08e0e7c2007-04-26 15:55:03 -0700421 /* insanity checks first */
422 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
423 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
426 _leave(" = 0 [no]");
427 return 0;
428 }
429
430 cookie->fid.vnode = ino;
431 cookie->fid.unique = dtype;
432 cookie->found = 1;
433
434 _leave(" = -1 [found]");
435 return -1;
David Howellsec268152007-04-26 15:49:28 -0700436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
David Howells08e0e7c2007-04-26 15:55:03 -0700439 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700440 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
David Howells08e0e7c2007-04-26 15:55:03 -0700442static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700443 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
David Howells260a9802007-04-26 15:59:35 -0700445 struct afs_lookup_cookie cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 unsigned fpos;
448 int ret;
449
David Howells08e0e7c2007-04-26 15:55:03 -0700450 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 as = dir->i_sb->s_fs_info;
453
454 /* search the directory */
455 cookie.name = dentry->d_name.name;
456 cookie.nlen = dentry->d_name.len;
457 cookie.fid.vid = as->volume->vid;
458 cookie.found = 0;
459
460 fpos = 0;
David Howells260a9802007-04-26 15:59:35 -0700461 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
David Howells00d3b7a2007-04-26 15:57:07 -0700462 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700464 _leave(" = %d [iter]", ret);
465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 ret = -ENOENT;
469 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700470 _leave(" = -ENOENT [not found]");
471 return -ENOENT;
472 }
473
474 *fid = cookie.fid;
475 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
476 return 0;
477}
478
479/*
480 * look up an entry in a directory
481 */
David Howells260a9802007-04-26 15:59:35 -0700482static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
483 struct nameidata *nd)
David Howells08e0e7c2007-04-26 15:55:03 -0700484{
485 struct afs_vnode *vnode;
486 struct afs_fid fid;
487 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700488 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700489 int ret;
490
David Howells260a9802007-04-26 15:59:35 -0700491 vnode = AFS_FS_I(dir);
492
David Howells416351f2007-05-09 02:33:45 -0700493 _enter("{%x:%u},%p{%s},",
David Howells260a9802007-04-26 15:59:35 -0700494 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
495
496 ASSERTCMP(dentry->d_inode, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700497
David Howells45222b92007-05-10 22:22:20 -0700498 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700499 _leave(" = -ENAMETOOLONG");
500 return ERR_PTR(-ENAMETOOLONG);
501 }
502
David Howells08e0e7c2007-04-26 15:55:03 -0700503 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
504 _leave(" = -ESTALE");
505 return ERR_PTR(-ESTALE);
506 }
507
David Howells00d3b7a2007-04-26 15:57:07 -0700508 key = afs_request_key(vnode->volume->cell);
509 if (IS_ERR(key)) {
510 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800511 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700512 }
513
David Howells260a9802007-04-26 15:59:35 -0700514 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700515 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700516 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700517 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return ERR_PTR(ret);
519 }
520
David Howells260a9802007-04-26 15:59:35 -0700521 ret = afs_do_lookup(dir, dentry, &fid, key);
522 if (ret < 0) {
523 key_put(key);
524 if (ret == -ENOENT) {
525 d_add(dentry, NULL);
526 _leave(" = NULL [negative]");
527 return NULL;
528 }
529 _leave(" = %d [do]", ret);
530 return ERR_PTR(ret);
531 }
532 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700535 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700536 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700537 if (IS_ERR(inode)) {
538 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800539 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
542 dentry->d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 d_add(dentry, inode);
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500545 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%llu }",
David Howells08e0e7c2007-04-26 15:55:03 -0700546 fid.vnode,
547 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dentry->d_inode->i_ino,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500549 (unsigned long long)dentry->d_inode->i_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/*
555 * check that a dentry lookup hit has found a valid entry
556 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
557 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
560{
David Howells260a9802007-04-26 15:59:35 -0700561 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100562 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700564 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700565 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int ret;
567
David Howells08e0e7c2007-04-26 15:55:03 -0700568 vnode = AFS_FS_I(dentry->d_inode);
569
David Howells260a9802007-04-26 15:59:35 -0700570 if (dentry->d_inode)
571 _enter("{v={%x:%u} n=%s fl=%lx},",
572 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
573 vnode->flags);
574 else
575 _enter("{neg n=%s}", dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Howells260a9802007-04-26 15:59:35 -0700577 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700578 if (IS_ERR(key))
579 key = NULL;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700582 parent = dget_parent(dentry);
David Howells260a9802007-04-26 15:59:35 -0700583 if (!parent->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto out_bad;
585
David Howells260a9802007-04-26 15:59:35 -0700586 dir = AFS_FS_I(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
David Howells260a9802007-04-26 15:59:35 -0700588 /* validate the parent directory */
589 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
590 afs_validate(dir, key);
591
592 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 _debug("%s: parent dir deleted", dentry->d_name.name);
594 goto out_bad;
595 }
596
David Howells260a9802007-04-26 15:59:35 -0700597 dir_version = (void *) (unsigned long) dir->status.data_version;
598 if (dentry->d_fsdata == dir_version)
599 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
David Howells260a9802007-04-26 15:59:35 -0700601 _debug("dir modified");
602
603 /* search the directory for this vnode */
604 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
605 switch (ret) {
606 case 0:
607 /* the filename maps to something */
608 if (!dentry->d_inode)
609 goto out_bad;
610 if (is_bad_inode(dentry->d_inode)) {
611 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
612 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto out_bad;
614 }
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* if the vnode ID has changed, then the dirent points to a
617 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700618 if (fid.vnode != vnode->fid.vnode) {
619 _debug("%s: dirent changed [%u != %u]",
620 dentry->d_name.name, fid.vnode,
621 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto not_found;
623 }
624
625 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700626 * been deleted and replaced, and the original vnode ID has
627 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700628 if (fid.unique != vnode->fid.unique) {
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500629 _debug("%s: file deleted (uq %u -> %u I:%llu)",
David Howells08e0e7c2007-04-26 15:55:03 -0700630 dentry->d_name.name, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500631 vnode->fid.unique,
632 (unsigned long long)dentry->d_inode->i_version);
David Howells08e0e7c2007-04-26 15:55:03 -0700633 spin_lock(&vnode->lock);
634 set_bit(AFS_VNODE_DELETED, &vnode->flags);
635 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700636 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
David Howells260a9802007-04-26 15:59:35 -0700638 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
David Howells260a9802007-04-26 15:59:35 -0700640 case -ENOENT:
641 /* the filename is unknown */
642 _debug("%s: dirent not found", dentry->d_name.name);
643 if (dentry->d_inode)
644 goto not_found;
645 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700646
David Howells260a9802007-04-26 15:59:35 -0700647 default:
648 _debug("failed to iterate dir %s: %d",
649 parent->d_name.name, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700650 goto out_bad;
651 }
652
David Howellsec268152007-04-26 15:49:28 -0700653out_valid:
David Howells260a9802007-04-26 15:59:35 -0700654 dentry->d_fsdata = dir_version;
655out_skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700657 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 _leave(" = 1 [valid]");
659 return 1;
660
661 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700662not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_lock(&dentry->d_lock);
664 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
665 spin_unlock(&dentry->d_lock);
666
David Howellsec268152007-04-26 15:49:28 -0700667out_bad:
David Howells260a9802007-04-26 15:59:35 -0700668 if (dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* don't unhash if we have submounts */
670 if (have_submounts(dentry))
David Howells260a9802007-04-26 15:59:35 -0700671 goto out_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700675 parent->d_name.name, dentry->d_name.name);
676 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700679 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 _leave(" = 0 [bad]");
682 return 0;
David Howellsec268152007-04-26 15:49:28 -0700683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
686 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
687 * sleep)
688 * - called from dput() when d_count is going to 0.
689 * - return 1 to request dentry be unhashed, 0 otherwise
690 */
691static int afs_d_delete(struct dentry *dentry)
692{
693 _enter("%s", dentry->d_name.name);
694
695 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
696 goto zap;
697
David Howells08e0e7c2007-04-26 15:55:03 -0700698 if (dentry->d_inode &&
699 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 _leave(" = 0 [keep]");
703 return 0;
704
David Howellsec268152007-04-26 15:49:28 -0700705zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 _leave(" = 1 [zap]");
707 return 1;
David Howellsec268152007-04-26 15:49:28 -0700708}
David Howells260a9802007-04-26 15:59:35 -0700709
710/*
711 * handle dentry release
712 */
713static void afs_d_release(struct dentry *dentry)
714{
715 _enter("%s", dentry->d_name.name);
716}
717
718/*
719 * create a directory on an AFS filesystem
720 */
721static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
722{
723 struct afs_file_status status;
724 struct afs_callback cb;
725 struct afs_server *server;
726 struct afs_vnode *dvnode, *vnode;
727 struct afs_fid fid;
728 struct inode *inode;
729 struct key *key;
730 int ret;
731
732 dvnode = AFS_FS_I(dir);
733
David Howells416351f2007-05-09 02:33:45 -0700734 _enter("{%x:%u},{%s},%o",
David Howells260a9802007-04-26 15:59:35 -0700735 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
736
737 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700738 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700739 goto error;
740
741 key = afs_request_key(dvnode->volume->cell);
742 if (IS_ERR(key)) {
743 ret = PTR_ERR(key);
744 goto error;
745 }
746
747 mode |= S_IFDIR;
748 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
749 mode, &fid, &status, &cb, &server);
750 if (ret < 0)
751 goto mkdir_error;
752
753 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
754 if (IS_ERR(inode)) {
755 /* ENOMEM at a really inconvenient time - just abandon the new
756 * directory on the server */
757 ret = PTR_ERR(inode);
758 goto iget_error;
759 }
760
761 /* apply the status report we've got for the new vnode */
762 vnode = AFS_FS_I(inode);
763 spin_lock(&vnode->lock);
764 vnode->update_cnt++;
765 spin_unlock(&vnode->lock);
766 afs_vnode_finalise_status_update(vnode, server);
767 afs_put_server(server);
768
769 d_instantiate(dentry, inode);
770 if (d_unhashed(dentry)) {
771 _debug("not hashed");
772 d_rehash(dentry);
773 }
774 key_put(key);
775 _leave(" = 0");
776 return 0;
777
778iget_error:
779 afs_put_server(server);
780mkdir_error:
781 key_put(key);
782error:
783 d_drop(dentry);
784 _leave(" = %d", ret);
785 return ret;
786}
787
788/*
789 * remove a directory from an AFS filesystem
790 */
791static int afs_rmdir(struct inode *dir, struct dentry *dentry)
792{
793 struct afs_vnode *dvnode, *vnode;
794 struct key *key;
795 int ret;
796
797 dvnode = AFS_FS_I(dir);
798
David Howells416351f2007-05-09 02:33:45 -0700799 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700800 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
801
802 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700803 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700804 goto error;
805
806 key = afs_request_key(dvnode->volume->cell);
807 if (IS_ERR(key)) {
808 ret = PTR_ERR(key);
809 goto error;
810 }
811
812 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
813 if (ret < 0)
814 goto rmdir_error;
815
816 if (dentry->d_inode) {
817 vnode = AFS_FS_I(dentry->d_inode);
818 clear_nlink(&vnode->vfs_inode);
819 set_bit(AFS_VNODE_DELETED, &vnode->flags);
820 afs_discard_callback_on_delete(vnode);
821 }
822
823 key_put(key);
824 _leave(" = 0");
825 return 0;
826
827rmdir_error:
828 key_put(key);
829error:
830 _leave(" = %d", ret);
831 return ret;
832}
833
834/*
835 * remove a file from an AFS filesystem
836 */
837static int afs_unlink(struct inode *dir, struct dentry *dentry)
838{
839 struct afs_vnode *dvnode, *vnode;
840 struct key *key;
841 int ret;
842
843 dvnode = AFS_FS_I(dir);
844
David Howells416351f2007-05-09 02:33:45 -0700845 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700846 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
847
848 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700849 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700850 goto error;
851
852 key = afs_request_key(dvnode->volume->cell);
853 if (IS_ERR(key)) {
854 ret = PTR_ERR(key);
855 goto error;
856 }
857
858 if (dentry->d_inode) {
859 vnode = AFS_FS_I(dentry->d_inode);
860
861 /* make sure we have a callback promise on the victim */
862 ret = afs_validate(vnode, key);
863 if (ret < 0)
864 goto error;
865 }
866
867 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
868 if (ret < 0)
869 goto remove_error;
870
871 if (dentry->d_inode) {
872 /* if the file wasn't deleted due to excess hard links, the
873 * fileserver will break the callback promise on the file - if
874 * it had one - before it returns to us, and if it was deleted,
875 * it won't
876 *
877 * however, if we didn't have a callback promise outstanding,
878 * or it was outstanding on a different server, then it won't
879 * break it either...
880 */
881 vnode = AFS_FS_I(dentry->d_inode);
882 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
883 _debug("AFS_VNODE_DELETED");
884 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
885 _debug("AFS_VNODE_CB_BROKEN");
886 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
887 ret = afs_validate(vnode, key);
888 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
889 }
890
891 key_put(key);
892 _leave(" = 0");
893 return 0;
894
895remove_error:
896 key_put(key);
897error:
898 _leave(" = %d", ret);
899 return ret;
900}
901
902/*
903 * create a regular file on an AFS filesystem
904 */
905static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
906 struct nameidata *nd)
907{
908 struct afs_file_status status;
909 struct afs_callback cb;
910 struct afs_server *server;
911 struct afs_vnode *dvnode, *vnode;
912 struct afs_fid fid;
913 struct inode *inode;
914 struct key *key;
915 int ret;
916
917 dvnode = AFS_FS_I(dir);
918
David Howells416351f2007-05-09 02:33:45 -0700919 _enter("{%x:%u},{%s},%o,",
David Howells260a9802007-04-26 15:59:35 -0700920 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
921
922 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700923 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700924 goto error;
925
926 key = afs_request_key(dvnode->volume->cell);
927 if (IS_ERR(key)) {
928 ret = PTR_ERR(key);
929 goto error;
930 }
931
932 mode |= S_IFREG;
933 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
934 mode, &fid, &status, &cb, &server);
935 if (ret < 0)
936 goto create_error;
937
938 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
939 if (IS_ERR(inode)) {
940 /* ENOMEM at a really inconvenient time - just abandon the new
941 * directory on the server */
942 ret = PTR_ERR(inode);
943 goto iget_error;
944 }
945
946 /* apply the status report we've got for the new vnode */
947 vnode = AFS_FS_I(inode);
948 spin_lock(&vnode->lock);
949 vnode->update_cnt++;
950 spin_unlock(&vnode->lock);
951 afs_vnode_finalise_status_update(vnode, server);
952 afs_put_server(server);
953
954 d_instantiate(dentry, inode);
955 if (d_unhashed(dentry)) {
956 _debug("not hashed");
957 d_rehash(dentry);
958 }
959 key_put(key);
960 _leave(" = 0");
961 return 0;
962
963iget_error:
964 afs_put_server(server);
965create_error:
966 key_put(key);
967error:
968 d_drop(dentry);
969 _leave(" = %d", ret);
970 return ret;
971}
972
973/*
974 * create a hard link between files in an AFS filesystem
975 */
976static int afs_link(struct dentry *from, struct inode *dir,
977 struct dentry *dentry)
978{
979 struct afs_vnode *dvnode, *vnode;
980 struct key *key;
981 int ret;
982
983 vnode = AFS_FS_I(from->d_inode);
984 dvnode = AFS_FS_I(dir);
985
David Howells416351f2007-05-09 02:33:45 -0700986 _enter("{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700987 vnode->fid.vid, vnode->fid.vnode,
988 dvnode->fid.vid, dvnode->fid.vnode,
989 dentry->d_name.name);
990
991 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700992 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700993 goto error;
994
995 key = afs_request_key(dvnode->volume->cell);
996 if (IS_ERR(key)) {
997 ret = PTR_ERR(key);
998 goto error;
999 }
1000
1001 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1002 if (ret < 0)
1003 goto link_error;
1004
1005 atomic_inc(&vnode->vfs_inode.i_count);
1006 d_instantiate(dentry, &vnode->vfs_inode);
1007 key_put(key);
1008 _leave(" = 0");
1009 return 0;
1010
1011link_error:
1012 key_put(key);
1013error:
1014 d_drop(dentry);
1015 _leave(" = %d", ret);
1016 return ret;
1017}
1018
1019/*
1020 * create a symlink in an AFS filesystem
1021 */
1022static int afs_symlink(struct inode *dir, struct dentry *dentry,
1023 const char *content)
1024{
1025 struct afs_file_status status;
1026 struct afs_server *server;
1027 struct afs_vnode *dvnode, *vnode;
1028 struct afs_fid fid;
1029 struct inode *inode;
1030 struct key *key;
1031 int ret;
1032
1033 dvnode = AFS_FS_I(dir);
1034
David Howells416351f2007-05-09 02:33:45 -07001035 _enter("{%x:%u},{%s},%s",
David Howells260a9802007-04-26 15:59:35 -07001036 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1037 content);
1038
1039 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001040 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001041 goto error;
1042
1043 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001044 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001045 goto error;
1046
1047 key = afs_request_key(dvnode->volume->cell);
1048 if (IS_ERR(key)) {
1049 ret = PTR_ERR(key);
1050 goto error;
1051 }
1052
1053 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1054 &fid, &status, &server);
1055 if (ret < 0)
1056 goto create_error;
1057
1058 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1059 if (IS_ERR(inode)) {
1060 /* ENOMEM at a really inconvenient time - just abandon the new
1061 * directory on the server */
1062 ret = PTR_ERR(inode);
1063 goto iget_error;
1064 }
1065
1066 /* apply the status report we've got for the new vnode */
1067 vnode = AFS_FS_I(inode);
1068 spin_lock(&vnode->lock);
1069 vnode->update_cnt++;
1070 spin_unlock(&vnode->lock);
1071 afs_vnode_finalise_status_update(vnode, server);
1072 afs_put_server(server);
1073
1074 d_instantiate(dentry, inode);
1075 if (d_unhashed(dentry)) {
1076 _debug("not hashed");
1077 d_rehash(dentry);
1078 }
1079 key_put(key);
1080 _leave(" = 0");
1081 return 0;
1082
1083iget_error:
1084 afs_put_server(server);
1085create_error:
1086 key_put(key);
1087error:
1088 d_drop(dentry);
1089 _leave(" = %d", ret);
1090 return ret;
1091}
1092
1093/*
1094 * rename a file in an AFS filesystem and/or move it between directories
1095 */
1096static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1097 struct inode *new_dir, struct dentry *new_dentry)
1098{
1099 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1100 struct key *key;
1101 int ret;
1102
1103 vnode = AFS_FS_I(old_dentry->d_inode);
1104 orig_dvnode = AFS_FS_I(old_dir);
1105 new_dvnode = AFS_FS_I(new_dir);
1106
David Howells416351f2007-05-09 02:33:45 -07001107 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001108 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1109 vnode->fid.vid, vnode->fid.vnode,
1110 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1111 new_dentry->d_name.name);
1112
1113 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001114 if (new_dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001115 goto error;
1116
1117 key = afs_request_key(orig_dvnode->volume->cell);
1118 if (IS_ERR(key)) {
1119 ret = PTR_ERR(key);
1120 goto error;
1121 }
1122
1123 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1124 old_dentry->d_name.name,
1125 new_dentry->d_name.name);
1126 if (ret < 0)
1127 goto rename_error;
1128 key_put(key);
1129 _leave(" = 0");
1130 return 0;
1131
1132rename_error:
1133 key_put(key);
1134error:
1135 d_drop(new_dentry);
1136 _leave(" = %d", ret);
1137 return ret;
1138}