blob: e6a4ab980e316fce2c08df14b1eb3f301ed375f1 [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>
Nick Piggin34286d62011-01-07 17:49:57 +110016#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pagemap.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howells260a9802007-04-26 15:59:35 -070022static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23 struct nameidata *nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int afs_dir_open(struct inode *inode, struct file *file);
David Howells260a9802007-04-26 15:59:35 -070025static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
Nick Pigginfe15ce42011-01-07 17:49:23 +110027static int afs_d_delete(const struct dentry *dentry);
David Howells260a9802007-04-26 15:59:35 -070028static void afs_d_release(struct dentry *dentry);
29static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -070030 loff_t fpos, u64 ino, unsigned dtype);
David Howells260a9802007-04-26 15:59:35 -070031static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
32 struct nameidata *nd);
33static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
34static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35static int afs_unlink(struct inode *dir, struct dentry *dentry);
36static int afs_link(struct dentry *from, struct inode *dir,
37 struct dentry *dentry);
38static int afs_symlink(struct inode *dir, struct dentry *dentry,
39 const char *content);
40static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
41 struct inode *new_dir, struct dentry *new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080043const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070045 .release = afs_release,
David Howells260a9802007-04-26 15:59:35 -070046 .readdir = afs_readdir,
David Howellse8d6c552007-07-15 23:40:12 -070047 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020048 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Arjan van de Ven754661f2007-02-12 00:55:38 -080051const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070052 .create = afs_create,
53 .lookup = afs_lookup,
54 .link = afs_link,
55 .unlink = afs_unlink,
56 .symlink = afs_symlink,
57 .mkdir = afs_mkdir,
58 .rmdir = afs_rmdir,
59 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070060 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070061 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070062 .setattr = afs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Al Virod61dcce2011-01-12 20:04:20 -050065const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .d_revalidate = afs_d_revalidate,
67 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070068 .d_release = afs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71#define AFS_DIR_HASHTBL_SIZE 128
72#define AFS_DIR_DIRENT_SIZE 32
73#define AFS_DIRENT_PER_BLOCK 64
74
75union afs_dirent {
76 struct {
77 uint8_t valid;
78 uint8_t unused[1];
79 __be16 hash_next;
80 __be32 vnode;
81 __be32 unique;
82 uint8_t name[16];
83 uint8_t overflow[4]; /* if any char of the name (inc
84 * NUL) reaches here, consume
85 * the next dirent too */
86 } u;
87 uint8_t extended_name[32];
88};
89
90/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
91struct afs_dir_pagehdr {
92 __be16 npages;
93 __be16 magic;
94#define AFS_DIR_MAGIC htons(1234)
95 uint8_t nentries;
96 uint8_t bitmap[8];
97 uint8_t pad[19];
98};
99
100/* directory block layout */
101union afs_dir_block {
102
103 struct afs_dir_pagehdr pagehdr;
104
105 struct {
106 struct afs_dir_pagehdr pagehdr;
107 uint8_t alloc_ctrs[128];
108 /* dir hash table */
109 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
110 } hdr;
111
112 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
113};
114
115/* layout on a linux VM page */
116struct afs_dir_page {
117 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
118};
119
David Howells260a9802007-04-26 15:59:35 -0700120struct afs_lookup_cookie {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct afs_fid fid;
122 const char *name;
123 size_t nlen;
124 int found;
125};
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * check that a directory page is valid
129 */
130static inline void afs_dir_check_page(struct inode *dir, struct page *page)
131{
132 struct afs_dir_page *dbuf;
133 loff_t latter;
134 int tmp, qty;
135
136#if 0
137 /* check the page count */
138 qty = desc.size / sizeof(dbuf->blocks[0]);
139 if (qty == 0)
140 goto error;
141
David Howells08e0e7c2007-04-26 15:55:03 -0700142 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700144 __func__, dir->i_ino, qty,
David Howells08e0e7c2007-04-26 15:55:03 -0700145 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto error;
147 }
148#endif
149
150 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800151 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (latter >= PAGE_SIZE)
153 qty = PAGE_SIZE;
154 else
155 qty = latter;
156 qty /= sizeof(union afs_dir_block);
157
158 /* check them */
159 dbuf = page_address(page);
160 for (tmp = 0; tmp < qty; tmp++) {
161 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
162 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700163 __func__, dir->i_ino, tmp, qty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ntohs(dbuf->blocks[tmp].pagehdr.magic));
165 goto error;
166 }
167 }
168
169 SetPageChecked(page);
170 return;
171
David Howellsec268152007-04-26 15:49:28 -0700172error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 SetPageChecked(page);
174 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
178 * discard a page cached in the pagecache
179 */
180static inline void afs_dir_put_page(struct page *page)
181{
182 kunmap(page);
183 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * get a page into the pagecache
188 */
David Howells00d3b7a2007-04-26 15:57:07 -0700189static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
190 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 _enter("{%lu},%lu", dir->i_ino, index);
194
Al Virof6d335c2010-05-21 15:27:09 +0100195 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!PageChecked(page))
199 afs_dir_check_page(dir, page);
200 if (PageError(page))
201 goto fail;
202 }
203 return page;
204
David Howellsec268152007-04-26 15:49:28 -0700205fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700207 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * open an AFS directory file
213 */
214static int afs_dir_open(struct inode *inode, struct file *file)
215{
216 _enter("{%lu}", inode->i_ino);
217
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700218 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
219 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howells08e0e7c2007-04-26 15:55:03 -0700221 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -ENOENT;
223
David Howells00d3b7a2007-04-26 15:57:07 -0700224 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
228 * deal with one block in an AFS directory
229 */
230static int afs_dir_iterate_block(unsigned *fpos,
231 union afs_dir_block *block,
232 unsigned blkoff,
233 void *cookie,
234 filldir_t filldir)
235{
236 union afs_dirent *dire;
237 unsigned offset, next, curr;
238 size_t nlen;
239 int tmp, ret;
240
241 _enter("%u,%x,%p,,",*fpos,blkoff,block);
242
243 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
244
245 /* walk through the block, an entry at a time */
246 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
247 offset < AFS_DIRENT_PER_BLOCK;
248 offset = next
249 ) {
250 next = offset + 1;
251
252 /* skip entries marked unused in the bitmap */
253 if (!(block->pagehdr.bitmap[offset / 8] &
254 (1 << (offset % 8)))) {
David Howells08e0e7c2007-04-26 15:55:03 -0700255 _debug("ENT[%Zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 blkoff / sizeof(union afs_dir_block), offset);
257 if (offset >= curr)
258 *fpos = blkoff +
259 next * sizeof(union afs_dirent);
260 continue;
261 }
262
263 /* got a valid entry */
264 dire = &block->dirents[offset];
265 nlen = strnlen(dire->u.name,
266 sizeof(*block) -
267 offset * sizeof(union afs_dirent));
268
David Howells08e0e7c2007-04-26 15:55:03 -0700269 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 blkoff / sizeof(union afs_dir_block), offset,
271 (offset < curr ? "skip" : "fill"),
272 nlen, dire->u.name);
273
274 /* work out where the next possible entry is */
275 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
276 if (next >= AFS_DIRENT_PER_BLOCK) {
277 _debug("ENT[%Zu.%u]:"
278 " %u travelled beyond end dir block"
David Howells08e0e7c2007-04-26 15:55:03 -0700279 " (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 blkoff / sizeof(union afs_dir_block),
281 offset, next, tmp, nlen);
282 return -EIO;
283 }
284 if (!(block->pagehdr.bitmap[next / 8] &
285 (1 << (next % 8)))) {
286 _debug("ENT[%Zu.%u]:"
David Howells08e0e7c2007-04-26 15:55:03 -0700287 " %u unmarked extension (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 blkoff / sizeof(union afs_dir_block),
289 offset, next, tmp, nlen);
290 return -EIO;
291 }
292
David Howells08e0e7c2007-04-26 15:55:03 -0700293 _debug("ENT[%Zu.%u]: ext %u/%Zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 blkoff / sizeof(union afs_dir_block),
295 next, tmp, nlen);
296 next++;
297 }
298
299 /* skip if starts before the current position */
300 if (offset < curr)
301 continue;
302
303 /* found the next entry */
304 ret = filldir(cookie,
305 dire->u.name,
306 nlen,
307 blkoff + offset * sizeof(union afs_dirent),
308 ntohl(dire->u.vnode),
David Howells260a9802007-04-26 15:59:35 -0700309 filldir == afs_lookup_filldir ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ntohl(dire->u.unique) : DT_UNKNOWN);
311 if (ret < 0) {
312 _leave(" = 0 [full]");
313 return 0;
314 }
315
316 *fpos = blkoff + next * sizeof(union afs_dirent);
317 }
318
319 _leave(" = 1 [more]");
320 return 1;
David Howellsec268152007-04-26 15:49:28 -0700321}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
David Howells08e0e7c2007-04-26 15:55:03 -0700324 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
David Howells00d3b7a2007-04-26 15:57:07 -0700327 filldir_t filldir, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
David Howells08e0e7c2007-04-26 15:55:03 -0700329 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct afs_dir_page *dbuf;
331 struct page *page;
332 unsigned blkoff, limit;
333 int ret;
334
335 _enter("{%lu},%u,,", dir->i_ino, *fpos);
336
David Howells08e0e7c2007-04-26 15:55:03 -0700337 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 _leave(" = -ESTALE");
339 return -ESTALE;
340 }
341
342 /* round the file position up to the next entry boundary */
343 *fpos += sizeof(union afs_dirent) - 1;
344 *fpos &= ~(sizeof(union afs_dirent) - 1);
345
346 /* walk through the blocks in sequence */
347 ret = 0;
348 while (*fpos < dir->i_size) {
349 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
350
351 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700352 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (IS_ERR(page)) {
354 ret = PTR_ERR(page);
355 break;
356 }
357
358 limit = blkoff & ~(PAGE_SIZE - 1);
359
360 dbuf = page_address(page);
361
362 /* deal with the individual blocks stashed on this page */
363 do {
364 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
365 sizeof(union afs_dir_block)];
366 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
367 cookie, filldir);
368 if (ret != 1) {
369 afs_dir_put_page(page);
370 goto out;
371 }
372
373 blkoff += sizeof(union afs_dir_block);
374
375 } while (*fpos < dir->i_size && blkoff < limit);
376
377 afs_dir_put_page(page);
378 ret = 0;
379 }
380
David Howellsec268152007-04-26 15:49:28 -0700381out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 _leave(" = %d", ret);
383 return ret;
David Howellsec268152007-04-26 15:49:28 -0700384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
387 * read an AFS directory
388 */
David Howells260a9802007-04-26 15:59:35 -0700389static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned fpos;
392 int ret;
393
David Howells08e0e7c2007-04-26 15:55:03 -0700394 _enter("{%Ld,{%lu}}",
395 file->f_pos, file->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells00d3b7a2007-04-26 15:57:07 -0700397 ASSERT(file->private_data != NULL);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 fpos = file->f_pos;
David Howells08e0e7c2007-04-26 15:55:03 -0700400 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
David Howells00d3b7a2007-04-26 15:57:07 -0700401 cookie, filldir, file->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 file->f_pos = fpos;
403
404 _leave(" = %d", ret);
405 return ret;
David Howellsec268152007-04-26 15:49:28 -0700406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * search the directory for a name
410 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
411 * uniquifier through dtype
412 */
David Howells260a9802007-04-26 15:59:35 -0700413static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
414 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
David Howells260a9802007-04-26 15:59:35 -0700416 struct afs_lookup_cookie *cookie = _cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
David Howells08e0e7c2007-04-26 15:55:03 -0700418 _enter("{%s,%Zu},%s,%u,,%llu,%u",
David S. Millerba3e0e12007-04-26 16:06:22 -0700419 cookie->name, cookie->nlen, name, nlen,
420 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Howells08e0e7c2007-04-26 15:55:03 -0700422 /* insanity checks first */
423 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
424 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
427 _leave(" = 0 [no]");
428 return 0;
429 }
430
431 cookie->fid.vnode = ino;
432 cookie->fid.unique = dtype;
433 cookie->found = 1;
434
435 _leave(" = -1 [found]");
436 return -1;
David Howellsec268152007-04-26 15:49:28 -0700437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
David Howells08e0e7c2007-04-26 15:55:03 -0700440 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700441 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
David Howells08e0e7c2007-04-26 15:55:03 -0700443static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700444 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
David Howells260a9802007-04-26 15:59:35 -0700446 struct afs_lookup_cookie cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned fpos;
449 int ret;
450
David Howells08e0e7c2007-04-26 15:55:03 -0700451 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 as = dir->i_sb->s_fs_info;
454
455 /* search the directory */
456 cookie.name = dentry->d_name.name;
457 cookie.nlen = dentry->d_name.len;
458 cookie.fid.vid = as->volume->vid;
459 cookie.found = 0;
460
461 fpos = 0;
David Howells260a9802007-04-26 15:59:35 -0700462 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
David Howells00d3b7a2007-04-26 15:57:07 -0700463 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700465 _leave(" = %d [iter]", ret);
466 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 ret = -ENOENT;
470 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700471 _leave(" = -ENOENT [not found]");
472 return -ENOENT;
473 }
474
475 *fid = cookie.fid;
476 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
477 return 0;
478}
479
480/*
wangleibec5eb62010-08-11 09:38:04 +0100481 * Try to auto mount the mountpoint with pseudo directory, if the autocell
482 * operation is setted.
483 */
484static struct inode *afs_try_auto_mntpt(
485 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
486 struct afs_fid *fid)
487{
488 const char *devname = dentry->d_name.name;
489 struct afs_vnode *vnode = AFS_FS_I(dir);
490 struct inode *inode;
491
492 _enter("%d, %p{%s}, {%x:%u}, %p",
493 ret, dentry, devname, vnode->fid.vid, vnode->fid.vnode, key);
494
495 if (ret != -ENOENT ||
496 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
497 goto out;
498
499 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
500 if (IS_ERR(inode)) {
501 ret = PTR_ERR(inode);
502 goto out;
503 }
504
505 *fid = AFS_FS_I(inode)->fid;
506 _leave("= %p", inode);
507 return inode;
508
509out:
510 _leave("= %d", ret);
511 return ERR_PTR(ret);
512}
513
514/*
David Howells08e0e7c2007-04-26 15:55:03 -0700515 * look up an entry in a directory
516 */
David Howells260a9802007-04-26 15:59:35 -0700517static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
518 struct nameidata *nd)
David Howells08e0e7c2007-04-26 15:55:03 -0700519{
520 struct afs_vnode *vnode;
521 struct afs_fid fid;
522 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700523 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700524 int ret;
525
David Howells260a9802007-04-26 15:59:35 -0700526 vnode = AFS_FS_I(dir);
527
David Howells416351f2007-05-09 02:33:45 -0700528 _enter("{%x:%u},%p{%s},",
David Howells260a9802007-04-26 15:59:35 -0700529 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
530
531 ASSERTCMP(dentry->d_inode, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700532
David Howells45222b92007-05-10 22:22:20 -0700533 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700534 _leave(" = -ENAMETOOLONG");
535 return ERR_PTR(-ENAMETOOLONG);
536 }
537
David Howells08e0e7c2007-04-26 15:55:03 -0700538 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
539 _leave(" = -ESTALE");
540 return ERR_PTR(-ESTALE);
541 }
542
David Howells00d3b7a2007-04-26 15:57:07 -0700543 key = afs_request_key(vnode->volume->cell);
544 if (IS_ERR(key)) {
545 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800546 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700547 }
548
David Howells260a9802007-04-26 15:59:35 -0700549 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700550 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700551 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700552 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return ERR_PTR(ret);
554 }
555
David Howells260a9802007-04-26 15:59:35 -0700556 ret = afs_do_lookup(dir, dentry, &fid, key);
557 if (ret < 0) {
wangleibec5eb62010-08-11 09:38:04 +0100558 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
559 if (!IS_ERR(inode)) {
560 key_put(key);
561 goto success;
562 }
563
564 ret = PTR_ERR(inode);
David Howells260a9802007-04-26 15:59:35 -0700565 key_put(key);
566 if (ret == -ENOENT) {
567 d_add(dentry, NULL);
568 _leave(" = NULL [negative]");
569 return NULL;
570 }
571 _leave(" = %d [do]", ret);
572 return ERR_PTR(ret);
573 }
574 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700577 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700578 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700579 if (IS_ERR(inode)) {
580 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800581 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
wangleibec5eb62010-08-11 09:38:04 +0100584success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 d_add(dentry, inode);
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500586 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%llu }",
David Howells08e0e7c2007-04-26 15:55:03 -0700587 fid.vnode,
588 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 dentry->d_inode->i_ino,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500590 (unsigned long long)dentry->d_inode->i_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * check that a dentry lookup hit has found a valid entry
597 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
598 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
601{
David Howells260a9802007-04-26 15:59:35 -0700602 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100603 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700605 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700606 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int ret;
608
Nick Piggin34286d62011-01-07 17:49:57 +1100609 if (nd->flags & LOOKUP_RCU)
610 return -ECHILD;
611
David Howells08e0e7c2007-04-26 15:55:03 -0700612 vnode = AFS_FS_I(dentry->d_inode);
613
David Howells260a9802007-04-26 15:59:35 -0700614 if (dentry->d_inode)
615 _enter("{v={%x:%u} n=%s fl=%lx},",
616 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
617 vnode->flags);
618 else
619 _enter("{neg n=%s}", dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
David Howells260a9802007-04-26 15:59:35 -0700621 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700622 if (IS_ERR(key))
623 key = NULL;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700626 parent = dget_parent(dentry);
David Howells260a9802007-04-26 15:59:35 -0700627 if (!parent->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto out_bad;
629
David Howells260a9802007-04-26 15:59:35 -0700630 dir = AFS_FS_I(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David Howells260a9802007-04-26 15:59:35 -0700632 /* validate the parent directory */
633 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
634 afs_validate(dir, key);
635
636 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 _debug("%s: parent dir deleted", dentry->d_name.name);
638 goto out_bad;
639 }
640
David Howells260a9802007-04-26 15:59:35 -0700641 dir_version = (void *) (unsigned long) dir->status.data_version;
642 if (dentry->d_fsdata == dir_version)
643 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
David Howells260a9802007-04-26 15:59:35 -0700645 _debug("dir modified");
646
647 /* search the directory for this vnode */
648 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
649 switch (ret) {
650 case 0:
651 /* the filename maps to something */
652 if (!dentry->d_inode)
653 goto out_bad;
654 if (is_bad_inode(dentry->d_inode)) {
655 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
656 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto out_bad;
658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* if the vnode ID has changed, then the dirent points to a
661 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700662 if (fid.vnode != vnode->fid.vnode) {
663 _debug("%s: dirent changed [%u != %u]",
664 dentry->d_name.name, fid.vnode,
665 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto not_found;
667 }
668
669 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700670 * been deleted and replaced, and the original vnode ID has
671 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700672 if (fid.unique != vnode->fid.unique) {
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500673 _debug("%s: file deleted (uq %u -> %u I:%llu)",
David Howells08e0e7c2007-04-26 15:55:03 -0700674 dentry->d_name.name, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500675 vnode->fid.unique,
676 (unsigned long long)dentry->d_inode->i_version);
David Howells08e0e7c2007-04-26 15:55:03 -0700677 spin_lock(&vnode->lock);
678 set_bit(AFS_VNODE_DELETED, &vnode->flags);
679 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700680 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
David Howells260a9802007-04-26 15:59:35 -0700682 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howells260a9802007-04-26 15:59:35 -0700684 case -ENOENT:
685 /* the filename is unknown */
686 _debug("%s: dirent not found", dentry->d_name.name);
687 if (dentry->d_inode)
688 goto not_found;
689 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700690
David Howells260a9802007-04-26 15:59:35 -0700691 default:
692 _debug("failed to iterate dir %s: %d",
693 parent->d_name.name, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700694 goto out_bad;
695 }
696
David Howellsec268152007-04-26 15:49:28 -0700697out_valid:
David Howells260a9802007-04-26 15:59:35 -0700698 dentry->d_fsdata = dir_version;
699out_skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700701 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 _leave(" = 1 [valid]");
703 return 1;
704
705 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700706not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock(&dentry->d_lock);
708 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
709 spin_unlock(&dentry->d_lock);
710
David Howellsec268152007-04-26 15:49:28 -0700711out_bad:
David Howells260a9802007-04-26 15:59:35 -0700712 if (dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* don't unhash if we have submounts */
714 if (have_submounts(dentry))
David Howells260a9802007-04-26 15:59:35 -0700715 goto out_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700719 parent->d_name.name, dentry->d_name.name);
720 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700723 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 _leave(" = 0 [bad]");
726 return 0;
David Howellsec268152007-04-26 15:49:28 -0700727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
731 * sleep)
732 * - called from dput() when d_count is going to 0.
733 * - return 1 to request dentry be unhashed, 0 otherwise
734 */
Nick Pigginfe15ce42011-01-07 17:49:23 +1100735static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 _enter("%s", dentry->d_name.name);
738
739 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
740 goto zap;
741
David Howells08e0e7c2007-04-26 15:55:03 -0700742 if (dentry->d_inode &&
wangleibec5eb62010-08-11 09:38:04 +0100743 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags) ||
744 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(dentry->d_inode)->flags)))
745 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 _leave(" = 0 [keep]");
748 return 0;
749
David Howellsec268152007-04-26 15:49:28 -0700750zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 _leave(" = 1 [zap]");
752 return 1;
David Howellsec268152007-04-26 15:49:28 -0700753}
David Howells260a9802007-04-26 15:59:35 -0700754
755/*
756 * handle dentry release
757 */
758static void afs_d_release(struct dentry *dentry)
759{
760 _enter("%s", dentry->d_name.name);
761}
762
763/*
764 * create a directory on an AFS filesystem
765 */
766static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
767{
768 struct afs_file_status status;
769 struct afs_callback cb;
770 struct afs_server *server;
771 struct afs_vnode *dvnode, *vnode;
772 struct afs_fid fid;
773 struct inode *inode;
774 struct key *key;
775 int ret;
776
777 dvnode = AFS_FS_I(dir);
778
David Howells416351f2007-05-09 02:33:45 -0700779 _enter("{%x:%u},{%s},%o",
David Howells260a9802007-04-26 15:59:35 -0700780 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
781
782 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700783 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700784 goto error;
785
786 key = afs_request_key(dvnode->volume->cell);
787 if (IS_ERR(key)) {
788 ret = PTR_ERR(key);
789 goto error;
790 }
791
792 mode |= S_IFDIR;
793 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
794 mode, &fid, &status, &cb, &server);
795 if (ret < 0)
796 goto mkdir_error;
797
798 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
799 if (IS_ERR(inode)) {
800 /* ENOMEM at a really inconvenient time - just abandon the new
801 * directory on the server */
802 ret = PTR_ERR(inode);
803 goto iget_error;
804 }
805
806 /* apply the status report we've got for the new vnode */
807 vnode = AFS_FS_I(inode);
808 spin_lock(&vnode->lock);
809 vnode->update_cnt++;
810 spin_unlock(&vnode->lock);
811 afs_vnode_finalise_status_update(vnode, server);
812 afs_put_server(server);
813
814 d_instantiate(dentry, inode);
815 if (d_unhashed(dentry)) {
816 _debug("not hashed");
817 d_rehash(dentry);
818 }
819 key_put(key);
820 _leave(" = 0");
821 return 0;
822
823iget_error:
824 afs_put_server(server);
825mkdir_error:
826 key_put(key);
827error:
828 d_drop(dentry);
829 _leave(" = %d", ret);
830 return ret;
831}
832
833/*
834 * remove a directory from an AFS filesystem
835 */
836static int afs_rmdir(struct inode *dir, struct dentry *dentry)
837{
838 struct afs_vnode *dvnode, *vnode;
839 struct key *key;
840 int ret;
841
842 dvnode = AFS_FS_I(dir);
843
David Howells416351f2007-05-09 02:33:45 -0700844 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700845 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
846
847 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700848 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700849 goto error;
850
851 key = afs_request_key(dvnode->volume->cell);
852 if (IS_ERR(key)) {
853 ret = PTR_ERR(key);
854 goto error;
855 }
856
857 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
858 if (ret < 0)
859 goto rmdir_error;
860
861 if (dentry->d_inode) {
862 vnode = AFS_FS_I(dentry->d_inode);
863 clear_nlink(&vnode->vfs_inode);
864 set_bit(AFS_VNODE_DELETED, &vnode->flags);
865 afs_discard_callback_on_delete(vnode);
866 }
867
868 key_put(key);
869 _leave(" = 0");
870 return 0;
871
872rmdir_error:
873 key_put(key);
874error:
875 _leave(" = %d", ret);
876 return ret;
877}
878
879/*
880 * remove a file from an AFS filesystem
881 */
882static int afs_unlink(struct inode *dir, struct dentry *dentry)
883{
884 struct afs_vnode *dvnode, *vnode;
885 struct key *key;
886 int ret;
887
888 dvnode = AFS_FS_I(dir);
889
David Howells416351f2007-05-09 02:33:45 -0700890 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700891 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
892
893 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700894 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700895 goto error;
896
897 key = afs_request_key(dvnode->volume->cell);
898 if (IS_ERR(key)) {
899 ret = PTR_ERR(key);
900 goto error;
901 }
902
903 if (dentry->d_inode) {
904 vnode = AFS_FS_I(dentry->d_inode);
905
906 /* make sure we have a callback promise on the victim */
907 ret = afs_validate(vnode, key);
908 if (ret < 0)
909 goto error;
910 }
911
912 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
913 if (ret < 0)
914 goto remove_error;
915
916 if (dentry->d_inode) {
917 /* if the file wasn't deleted due to excess hard links, the
918 * fileserver will break the callback promise on the file - if
919 * it had one - before it returns to us, and if it was deleted,
920 * it won't
921 *
922 * however, if we didn't have a callback promise outstanding,
923 * or it was outstanding on a different server, then it won't
924 * break it either...
925 */
926 vnode = AFS_FS_I(dentry->d_inode);
927 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
928 _debug("AFS_VNODE_DELETED");
929 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
930 _debug("AFS_VNODE_CB_BROKEN");
931 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
932 ret = afs_validate(vnode, key);
933 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
934 }
935
936 key_put(key);
937 _leave(" = 0");
938 return 0;
939
940remove_error:
941 key_put(key);
942error:
943 _leave(" = %d", ret);
944 return ret;
945}
946
947/*
948 * create a regular file on an AFS filesystem
949 */
950static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
951 struct nameidata *nd)
952{
953 struct afs_file_status status;
954 struct afs_callback cb;
955 struct afs_server *server;
956 struct afs_vnode *dvnode, *vnode;
957 struct afs_fid fid;
958 struct inode *inode;
959 struct key *key;
960 int ret;
961
962 dvnode = AFS_FS_I(dir);
963
David Howells416351f2007-05-09 02:33:45 -0700964 _enter("{%x:%u},{%s},%o,",
David Howells260a9802007-04-26 15:59:35 -0700965 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
966
967 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700968 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700969 goto error;
970
971 key = afs_request_key(dvnode->volume->cell);
972 if (IS_ERR(key)) {
973 ret = PTR_ERR(key);
974 goto error;
975 }
976
977 mode |= S_IFREG;
978 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
979 mode, &fid, &status, &cb, &server);
980 if (ret < 0)
981 goto create_error;
982
983 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
984 if (IS_ERR(inode)) {
985 /* ENOMEM at a really inconvenient time - just abandon the new
986 * directory on the server */
987 ret = PTR_ERR(inode);
988 goto iget_error;
989 }
990
991 /* apply the status report we've got for the new vnode */
992 vnode = AFS_FS_I(inode);
993 spin_lock(&vnode->lock);
994 vnode->update_cnt++;
995 spin_unlock(&vnode->lock);
996 afs_vnode_finalise_status_update(vnode, server);
997 afs_put_server(server);
998
999 d_instantiate(dentry, inode);
1000 if (d_unhashed(dentry)) {
1001 _debug("not hashed");
1002 d_rehash(dentry);
1003 }
1004 key_put(key);
1005 _leave(" = 0");
1006 return 0;
1007
1008iget_error:
1009 afs_put_server(server);
1010create_error:
1011 key_put(key);
1012error:
1013 d_drop(dentry);
1014 _leave(" = %d", ret);
1015 return ret;
1016}
1017
1018/*
1019 * create a hard link between files in an AFS filesystem
1020 */
1021static int afs_link(struct dentry *from, struct inode *dir,
1022 struct dentry *dentry)
1023{
1024 struct afs_vnode *dvnode, *vnode;
1025 struct key *key;
1026 int ret;
1027
1028 vnode = AFS_FS_I(from->d_inode);
1029 dvnode = AFS_FS_I(dir);
1030
David Howells416351f2007-05-09 02:33:45 -07001031 _enter("{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001032 vnode->fid.vid, vnode->fid.vnode,
1033 dvnode->fid.vid, dvnode->fid.vnode,
1034 dentry->d_name.name);
1035
1036 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001037 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001038 goto error;
1039
1040 key = afs_request_key(dvnode->volume->cell);
1041 if (IS_ERR(key)) {
1042 ret = PTR_ERR(key);
1043 goto error;
1044 }
1045
1046 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1047 if (ret < 0)
1048 goto link_error;
1049
Al Viro7de9c6e2010-10-23 11:11:40 -04001050 ihold(&vnode->vfs_inode);
David Howells260a9802007-04-26 15:59:35 -07001051 d_instantiate(dentry, &vnode->vfs_inode);
1052 key_put(key);
1053 _leave(" = 0");
1054 return 0;
1055
1056link_error:
1057 key_put(key);
1058error:
1059 d_drop(dentry);
1060 _leave(" = %d", ret);
1061 return ret;
1062}
1063
1064/*
1065 * create a symlink in an AFS filesystem
1066 */
1067static int afs_symlink(struct inode *dir, struct dentry *dentry,
1068 const char *content)
1069{
1070 struct afs_file_status status;
1071 struct afs_server *server;
1072 struct afs_vnode *dvnode, *vnode;
1073 struct afs_fid fid;
1074 struct inode *inode;
1075 struct key *key;
1076 int ret;
1077
1078 dvnode = AFS_FS_I(dir);
1079
David Howells416351f2007-05-09 02:33:45 -07001080 _enter("{%x:%u},{%s},%s",
David Howells260a9802007-04-26 15:59:35 -07001081 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1082 content);
1083
1084 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001085 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001086 goto error;
1087
1088 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001089 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001090 goto error;
1091
1092 key = afs_request_key(dvnode->volume->cell);
1093 if (IS_ERR(key)) {
1094 ret = PTR_ERR(key);
1095 goto error;
1096 }
1097
1098 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1099 &fid, &status, &server);
1100 if (ret < 0)
1101 goto create_error;
1102
1103 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1104 if (IS_ERR(inode)) {
1105 /* ENOMEM at a really inconvenient time - just abandon the new
1106 * directory on the server */
1107 ret = PTR_ERR(inode);
1108 goto iget_error;
1109 }
1110
1111 /* apply the status report we've got for the new vnode */
1112 vnode = AFS_FS_I(inode);
1113 spin_lock(&vnode->lock);
1114 vnode->update_cnt++;
1115 spin_unlock(&vnode->lock);
1116 afs_vnode_finalise_status_update(vnode, server);
1117 afs_put_server(server);
1118
1119 d_instantiate(dentry, inode);
1120 if (d_unhashed(dentry)) {
1121 _debug("not hashed");
1122 d_rehash(dentry);
1123 }
1124 key_put(key);
1125 _leave(" = 0");
1126 return 0;
1127
1128iget_error:
1129 afs_put_server(server);
1130create_error:
1131 key_put(key);
1132error:
1133 d_drop(dentry);
1134 _leave(" = %d", ret);
1135 return ret;
1136}
1137
1138/*
1139 * rename a file in an AFS filesystem and/or move it between directories
1140 */
1141static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1142 struct inode *new_dir, struct dentry *new_dentry)
1143{
1144 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1145 struct key *key;
1146 int ret;
1147
1148 vnode = AFS_FS_I(old_dentry->d_inode);
1149 orig_dvnode = AFS_FS_I(old_dir);
1150 new_dvnode = AFS_FS_I(new_dir);
1151
David Howells416351f2007-05-09 02:33:45 -07001152 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001153 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1154 vnode->fid.vid, vnode->fid.vnode,
1155 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1156 new_dentry->d_name.name);
1157
1158 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001159 if (new_dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001160 goto error;
1161
1162 key = afs_request_key(orig_dvnode->volume->cell);
1163 if (IS_ERR(key)) {
1164 ret = PTR_ERR(key);
1165 goto error;
1166 }
1167
1168 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1169 old_dentry->d_name.name,
1170 new_dentry->d_name.name);
1171 if (ret < 0)
1172 goto rename_error;
1173 key_put(key);
1174 _leave(" = 0");
1175 return 0;
1176
1177rename_error:
1178 key_put(key);
1179error:
1180 d_drop(new_dentry);
1181 _leave(" = %d", ret);
1182 return ret;
1183}