blob: 546c59522eb16e0307070102df305cc05766f419 [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>
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);
27static int afs_d_delete(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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Arjan van de Ven754661f2007-02-12 00:55:38 -080049const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070050 .create = afs_create,
51 .lookup = afs_lookup,
52 .link = afs_link,
53 .unlink = afs_unlink,
54 .symlink = afs_symlink,
55 .mkdir = afs_mkdir,
56 .rmdir = afs_rmdir,
57 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070058 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070059 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070060 .setattr = afs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63static struct dentry_operations afs_fs_dentry_operations = {
64 .d_revalidate = afs_d_revalidate,
65 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070066 .d_release = afs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69#define AFS_DIR_HASHTBL_SIZE 128
70#define AFS_DIR_DIRENT_SIZE 32
71#define AFS_DIRENT_PER_BLOCK 64
72
73union afs_dirent {
74 struct {
75 uint8_t valid;
76 uint8_t unused[1];
77 __be16 hash_next;
78 __be32 vnode;
79 __be32 unique;
80 uint8_t name[16];
81 uint8_t overflow[4]; /* if any char of the name (inc
82 * NUL) reaches here, consume
83 * the next dirent too */
84 } u;
85 uint8_t extended_name[32];
86};
87
88/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
89struct afs_dir_pagehdr {
90 __be16 npages;
91 __be16 magic;
92#define AFS_DIR_MAGIC htons(1234)
93 uint8_t nentries;
94 uint8_t bitmap[8];
95 uint8_t pad[19];
96};
97
98/* directory block layout */
99union afs_dir_block {
100
101 struct afs_dir_pagehdr pagehdr;
102
103 struct {
104 struct afs_dir_pagehdr pagehdr;
105 uint8_t alloc_ctrs[128];
106 /* dir hash table */
107 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
108 } hdr;
109
110 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
111};
112
113/* layout on a linux VM page */
114struct afs_dir_page {
115 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
116};
117
David Howells260a9802007-04-26 15:59:35 -0700118struct afs_lookup_cookie {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct afs_fid fid;
120 const char *name;
121 size_t nlen;
122 int found;
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * check that a directory page is valid
127 */
128static inline void afs_dir_check_page(struct inode *dir, struct page *page)
129{
130 struct afs_dir_page *dbuf;
131 loff_t latter;
132 int tmp, qty;
133
134#if 0
135 /* check the page count */
136 qty = desc.size / sizeof(dbuf->blocks[0]);
137 if (qty == 0)
138 goto error;
139
David Howells08e0e7c2007-04-26 15:55:03 -0700140 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
David Howells08e0e7c2007-04-26 15:55:03 -0700142 __FUNCTION__, dir->i_ino, qty,
143 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto error;
145 }
146#endif
147
148 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800149 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (latter >= PAGE_SIZE)
151 qty = PAGE_SIZE;
152 else
153 qty = latter;
154 qty /= sizeof(union afs_dir_block);
155
156 /* check them */
157 dbuf = page_address(page);
158 for (tmp = 0; tmp < qty; tmp++) {
159 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
160 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
161 __FUNCTION__, dir->i_ino, tmp, qty,
162 ntohs(dbuf->blocks[tmp].pagehdr.magic));
163 goto error;
164 }
165 }
166
167 SetPageChecked(page);
168 return;
169
David Howellsec268152007-04-26 15:49:28 -0700170error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 SetPageChecked(page);
172 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
176 * discard a page cached in the pagecache
177 */
178static inline void afs_dir_put_page(struct page *page)
179{
180 kunmap(page);
181 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
185 * get a page into the pagecache
186 */
David Howells00d3b7a2007-04-26 15:57:07 -0700187static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
188 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct page *page;
David Howells00d3b7a2007-04-26 15:57:07 -0700191 struct file file = {
192 .private_data = key,
193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 _enter("{%lu},%lu", dir->i_ino, index);
196
David Howells00d3b7a2007-04-26 15:57:07 -0700197 page = read_mapping_page(dir->i_mapping, index, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!PageChecked(page))
201 afs_dir_check_page(dir, page);
202 if (PageError(page))
203 goto fail;
204 }
205 return page;
206
David Howellsec268152007-04-26 15:49:28 -0700207fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700209 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * open an AFS directory file
215 */
216static int afs_dir_open(struct inode *inode, struct file *file)
217{
218 _enter("{%lu}", inode->i_ino);
219
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700220 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
221 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Howells08e0e7c2007-04-26 15:55:03 -0700223 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -ENOENT;
225
David Howells00d3b7a2007-04-26 15:57:07 -0700226 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * deal with one block in an AFS directory
231 */
232static int afs_dir_iterate_block(unsigned *fpos,
233 union afs_dir_block *block,
234 unsigned blkoff,
235 void *cookie,
236 filldir_t filldir)
237{
238 union afs_dirent *dire;
239 unsigned offset, next, curr;
240 size_t nlen;
241 int tmp, ret;
242
243 _enter("%u,%x,%p,,",*fpos,blkoff,block);
244
245 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
246
247 /* walk through the block, an entry at a time */
248 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
249 offset < AFS_DIRENT_PER_BLOCK;
250 offset = next
251 ) {
252 next = offset + 1;
253
254 /* skip entries marked unused in the bitmap */
255 if (!(block->pagehdr.bitmap[offset / 8] &
256 (1 << (offset % 8)))) {
David Howells08e0e7c2007-04-26 15:55:03 -0700257 _debug("ENT[%Zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 blkoff / sizeof(union afs_dir_block), offset);
259 if (offset >= curr)
260 *fpos = blkoff +
261 next * sizeof(union afs_dirent);
262 continue;
263 }
264
265 /* got a valid entry */
266 dire = &block->dirents[offset];
267 nlen = strnlen(dire->u.name,
268 sizeof(*block) -
269 offset * sizeof(union afs_dirent));
270
David Howells08e0e7c2007-04-26 15:55:03 -0700271 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 blkoff / sizeof(union afs_dir_block), offset,
273 (offset < curr ? "skip" : "fill"),
274 nlen, dire->u.name);
275
276 /* work out where the next possible entry is */
277 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
278 if (next >= AFS_DIRENT_PER_BLOCK) {
279 _debug("ENT[%Zu.%u]:"
280 " %u travelled beyond end dir block"
David Howells08e0e7c2007-04-26 15:55:03 -0700281 " (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 blkoff / sizeof(union afs_dir_block),
283 offset, next, tmp, nlen);
284 return -EIO;
285 }
286 if (!(block->pagehdr.bitmap[next / 8] &
287 (1 << (next % 8)))) {
288 _debug("ENT[%Zu.%u]:"
David Howells08e0e7c2007-04-26 15:55:03 -0700289 " %u unmarked extension (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 blkoff / sizeof(union afs_dir_block),
291 offset, next, tmp, nlen);
292 return -EIO;
293 }
294
David Howells08e0e7c2007-04-26 15:55:03 -0700295 _debug("ENT[%Zu.%u]: ext %u/%Zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 blkoff / sizeof(union afs_dir_block),
297 next, tmp, nlen);
298 next++;
299 }
300
301 /* skip if starts before the current position */
302 if (offset < curr)
303 continue;
304
305 /* found the next entry */
306 ret = filldir(cookie,
307 dire->u.name,
308 nlen,
309 blkoff + offset * sizeof(union afs_dirent),
310 ntohl(dire->u.vnode),
David Howells260a9802007-04-26 15:59:35 -0700311 filldir == afs_lookup_filldir ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ntohl(dire->u.unique) : DT_UNKNOWN);
313 if (ret < 0) {
314 _leave(" = 0 [full]");
315 return 0;
316 }
317
318 *fpos = blkoff + next * sizeof(union afs_dirent);
319 }
320
321 _leave(" = 1 [more]");
322 return 1;
David Howellsec268152007-04-26 15:49:28 -0700323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
David Howells08e0e7c2007-04-26 15:55:03 -0700326 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
David Howells00d3b7a2007-04-26 15:57:07 -0700329 filldir_t filldir, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
David Howells08e0e7c2007-04-26 15:55:03 -0700331 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct afs_dir_page *dbuf;
333 struct page *page;
334 unsigned blkoff, limit;
335 int ret;
336
337 _enter("{%lu},%u,,", dir->i_ino, *fpos);
338
David Howells08e0e7c2007-04-26 15:55:03 -0700339 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 _leave(" = -ESTALE");
341 return -ESTALE;
342 }
343
344 /* round the file position up to the next entry boundary */
345 *fpos += sizeof(union afs_dirent) - 1;
346 *fpos &= ~(sizeof(union afs_dirent) - 1);
347
348 /* walk through the blocks in sequence */
349 ret = 0;
350 while (*fpos < dir->i_size) {
351 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
352
353 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700354 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (IS_ERR(page)) {
356 ret = PTR_ERR(page);
357 break;
358 }
359
360 limit = blkoff & ~(PAGE_SIZE - 1);
361
362 dbuf = page_address(page);
363
364 /* deal with the individual blocks stashed on this page */
365 do {
366 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
367 sizeof(union afs_dir_block)];
368 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
369 cookie, filldir);
370 if (ret != 1) {
371 afs_dir_put_page(page);
372 goto out;
373 }
374
375 blkoff += sizeof(union afs_dir_block);
376
377 } while (*fpos < dir->i_size && blkoff < limit);
378
379 afs_dir_put_page(page);
380 ret = 0;
381 }
382
David Howellsec268152007-04-26 15:49:28 -0700383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 _leave(" = %d", ret);
385 return ret;
David Howellsec268152007-04-26 15:49:28 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
389 * read an AFS directory
390 */
David Howells260a9802007-04-26 15:59:35 -0700391static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 unsigned fpos;
394 int ret;
395
David Howells08e0e7c2007-04-26 15:55:03 -0700396 _enter("{%Ld,{%lu}}",
397 file->f_pos, file->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howells00d3b7a2007-04-26 15:57:07 -0700399 ASSERT(file->private_data != NULL);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 fpos = file->f_pos;
David Howells08e0e7c2007-04-26 15:55:03 -0700402 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
David Howells00d3b7a2007-04-26 15:57:07 -0700403 cookie, filldir, file->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 file->f_pos = fpos;
405
406 _leave(" = %d", ret);
407 return ret;
David Howellsec268152007-04-26 15:49:28 -0700408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
411 * search the directory for a name
412 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
413 * uniquifier through dtype
414 */
David Howells260a9802007-04-26 15:59:35 -0700415static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
416 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
David Howells260a9802007-04-26 15:59:35 -0700418 struct afs_lookup_cookie *cookie = _cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howells08e0e7c2007-04-26 15:55:03 -0700420 _enter("{%s,%Zu},%s,%u,,%llu,%u",
David S. Millerba3e0e12007-04-26 16:06:22 -0700421 cookie->name, cookie->nlen, name, nlen,
422 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David Howells08e0e7c2007-04-26 15:55:03 -0700424 /* insanity checks first */
425 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
426 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
429 _leave(" = 0 [no]");
430 return 0;
431 }
432
433 cookie->fid.vnode = ino;
434 cookie->fid.unique = dtype;
435 cookie->found = 1;
436
437 _leave(" = -1 [found]");
438 return -1;
David Howellsec268152007-04-26 15:49:28 -0700439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
David Howells08e0e7c2007-04-26 15:55:03 -0700442 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700443 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
David Howells08e0e7c2007-04-26 15:55:03 -0700445static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700446 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
David Howells260a9802007-04-26 15:59:35 -0700448 struct afs_lookup_cookie cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 unsigned fpos;
451 int ret;
452
David Howells08e0e7c2007-04-26 15:55:03 -0700453 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 as = dir->i_sb->s_fs_info;
456
457 /* search the directory */
458 cookie.name = dentry->d_name.name;
459 cookie.nlen = dentry->d_name.len;
460 cookie.fid.vid = as->volume->vid;
461 cookie.found = 0;
462
463 fpos = 0;
David Howells260a9802007-04-26 15:59:35 -0700464 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
David Howells00d3b7a2007-04-26 15:57:07 -0700465 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700467 _leave(" = %d [iter]", ret);
468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 ret = -ENOENT;
472 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700473 _leave(" = -ENOENT [not found]");
474 return -ENOENT;
475 }
476
477 *fid = cookie.fid;
478 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
479 return 0;
480}
481
482/*
483 * look up an entry in a directory
484 */
David Howells260a9802007-04-26 15:59:35 -0700485static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
486 struct nameidata *nd)
David Howells08e0e7c2007-04-26 15:55:03 -0700487{
488 struct afs_vnode *vnode;
489 struct afs_fid fid;
490 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700491 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700492 int ret;
493
David Howells260a9802007-04-26 15:59:35 -0700494 vnode = AFS_FS_I(dir);
495
David Howells416351f2007-05-09 02:33:45 -0700496 _enter("{%x:%u},%p{%s},",
David Howells260a9802007-04-26 15:59:35 -0700497 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
498
499 ASSERTCMP(dentry->d_inode, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700500
David Howells45222b92007-05-10 22:22:20 -0700501 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700502 _leave(" = -ENAMETOOLONG");
503 return ERR_PTR(-ENAMETOOLONG);
504 }
505
David Howells08e0e7c2007-04-26 15:55:03 -0700506 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
507 _leave(" = -ESTALE");
508 return ERR_PTR(-ESTALE);
509 }
510
David Howells00d3b7a2007-04-26 15:57:07 -0700511 key = afs_request_key(vnode->volume->cell);
512 if (IS_ERR(key)) {
513 _leave(" = %ld [key]", PTR_ERR(key));
514 return ERR_PTR(PTR_ERR(key));
515 }
516
David Howells260a9802007-04-26 15:59:35 -0700517 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700518 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700519 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700520 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return ERR_PTR(ret);
522 }
523
David Howells260a9802007-04-26 15:59:35 -0700524 ret = afs_do_lookup(dir, dentry, &fid, key);
525 if (ret < 0) {
526 key_put(key);
527 if (ret == -ENOENT) {
528 d_add(dentry, NULL);
529 _leave(" = NULL [negative]");
530 return NULL;
531 }
532 _leave(" = %d [do]", ret);
533 return ERR_PTR(ret);
534 }
535 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700538 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700539 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700540 if (IS_ERR(inode)) {
541 _leave(" = %ld", PTR_ERR(inode));
542 return ERR_PTR(PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
545 dentry->d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 d_add(dentry, inode);
548 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
David Howells08e0e7c2007-04-26 15:55:03 -0700549 fid.vnode,
550 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dentry->d_inode->i_ino,
552 dentry->d_inode->i_version);
553
554 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
558 * check that a dentry lookup hit has found a valid entry
559 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
560 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
562static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
563{
David Howells260a9802007-04-26 15:59:35 -0700564 struct afs_vnode *vnode, *dir;
David Howells08e0e7c2007-04-26 15:55:03 -0700565 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700567 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700568 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int ret;
570
David Howells08e0e7c2007-04-26 15:55:03 -0700571 vnode = AFS_FS_I(dentry->d_inode);
572
David Howells260a9802007-04-26 15:59:35 -0700573 if (dentry->d_inode)
574 _enter("{v={%x:%u} n=%s fl=%lx},",
575 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
576 vnode->flags);
577 else
578 _enter("{neg n=%s}", dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Howells260a9802007-04-26 15:59:35 -0700580 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700581 if (IS_ERR(key))
582 key = NULL;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700585 parent = dget_parent(dentry);
David Howells260a9802007-04-26 15:59:35 -0700586 if (!parent->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto out_bad;
588
David Howells260a9802007-04-26 15:59:35 -0700589 dir = AFS_FS_I(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
David Howells260a9802007-04-26 15:59:35 -0700591 /* validate the parent directory */
592 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
593 afs_validate(dir, key);
594
595 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 _debug("%s: parent dir deleted", dentry->d_name.name);
597 goto out_bad;
598 }
599
David Howells260a9802007-04-26 15:59:35 -0700600 dir_version = (void *) (unsigned long) dir->status.data_version;
601 if (dentry->d_fsdata == dir_version)
602 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
David Howells260a9802007-04-26 15:59:35 -0700604 _debug("dir modified");
605
606 /* search the directory for this vnode */
607 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
608 switch (ret) {
609 case 0:
610 /* the filename maps to something */
611 if (!dentry->d_inode)
612 goto out_bad;
613 if (is_bad_inode(dentry->d_inode)) {
614 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
615 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out_bad;
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* if the vnode ID has changed, then the dirent points to a
620 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700621 if (fid.vnode != vnode->fid.vnode) {
622 _debug("%s: dirent changed [%u != %u]",
623 dentry->d_name.name, fid.vnode,
624 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto not_found;
626 }
627
628 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700629 * been deleted and replaced, and the original vnode ID has
630 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700631 if (fid.unique != vnode->fid.unique) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 _debug("%s: file deleted (uq %u -> %u I:%lu)",
David Howells08e0e7c2007-04-26 15:55:03 -0700633 dentry->d_name.name, fid.unique,
David Howells260a9802007-04-26 15:59:35 -0700634 vnode->fid.unique, dentry->d_inode->i_version);
David Howells08e0e7c2007-04-26 15:55:03 -0700635 spin_lock(&vnode->lock);
636 set_bit(AFS_VNODE_DELETED, &vnode->flags);
637 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700638 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
David Howells260a9802007-04-26 15:59:35 -0700640 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
David Howells260a9802007-04-26 15:59:35 -0700642 case -ENOENT:
643 /* the filename is unknown */
644 _debug("%s: dirent not found", dentry->d_name.name);
645 if (dentry->d_inode)
646 goto not_found;
647 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700648
David Howells260a9802007-04-26 15:59:35 -0700649 default:
650 _debug("failed to iterate dir %s: %d",
651 parent->d_name.name, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700652 goto out_bad;
653 }
654
David Howellsec268152007-04-26 15:49:28 -0700655out_valid:
David Howells260a9802007-04-26 15:59:35 -0700656 dentry->d_fsdata = dir_version;
657out_skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700659 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 _leave(" = 1 [valid]");
661 return 1;
662
663 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700664not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock(&dentry->d_lock);
666 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
667 spin_unlock(&dentry->d_lock);
668
David Howellsec268152007-04-26 15:49:28 -0700669out_bad:
David Howells260a9802007-04-26 15:59:35 -0700670 if (dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* don't unhash if we have submounts */
672 if (have_submounts(dentry))
David Howells260a9802007-04-26 15:59:35 -0700673 goto out_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700677 parent->d_name.name, dentry->d_name.name);
678 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700681 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 _leave(" = 0 [bad]");
684 return 0;
David Howellsec268152007-04-26 15:49:28 -0700685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/*
688 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
689 * sleep)
690 * - called from dput() when d_count is going to 0.
691 * - return 1 to request dentry be unhashed, 0 otherwise
692 */
693static int afs_d_delete(struct dentry *dentry)
694{
695 _enter("%s", dentry->d_name.name);
696
697 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
698 goto zap;
699
David Howells08e0e7c2007-04-26 15:55:03 -0700700 if (dentry->d_inode &&
701 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 _leave(" = 0 [keep]");
705 return 0;
706
David Howellsec268152007-04-26 15:49:28 -0700707zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 _leave(" = 1 [zap]");
709 return 1;
David Howellsec268152007-04-26 15:49:28 -0700710}
David Howells260a9802007-04-26 15:59:35 -0700711
712/*
713 * handle dentry release
714 */
715static void afs_d_release(struct dentry *dentry)
716{
717 _enter("%s", dentry->d_name.name);
718}
719
720/*
721 * create a directory on an AFS filesystem
722 */
723static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
724{
725 struct afs_file_status status;
726 struct afs_callback cb;
727 struct afs_server *server;
728 struct afs_vnode *dvnode, *vnode;
729 struct afs_fid fid;
730 struct inode *inode;
731 struct key *key;
732 int ret;
733
734 dvnode = AFS_FS_I(dir);
735
David Howells416351f2007-05-09 02:33:45 -0700736 _enter("{%x:%u},{%s},%o",
David Howells260a9802007-04-26 15:59:35 -0700737 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
738
739 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700740 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700741 goto error;
742
743 key = afs_request_key(dvnode->volume->cell);
744 if (IS_ERR(key)) {
745 ret = PTR_ERR(key);
746 goto error;
747 }
748
749 mode |= S_IFDIR;
750 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
751 mode, &fid, &status, &cb, &server);
752 if (ret < 0)
753 goto mkdir_error;
754
755 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
756 if (IS_ERR(inode)) {
757 /* ENOMEM at a really inconvenient time - just abandon the new
758 * directory on the server */
759 ret = PTR_ERR(inode);
760 goto iget_error;
761 }
762
763 /* apply the status report we've got for the new vnode */
764 vnode = AFS_FS_I(inode);
765 spin_lock(&vnode->lock);
766 vnode->update_cnt++;
767 spin_unlock(&vnode->lock);
768 afs_vnode_finalise_status_update(vnode, server);
769 afs_put_server(server);
770
771 d_instantiate(dentry, inode);
772 if (d_unhashed(dentry)) {
773 _debug("not hashed");
774 d_rehash(dentry);
775 }
776 key_put(key);
777 _leave(" = 0");
778 return 0;
779
780iget_error:
781 afs_put_server(server);
782mkdir_error:
783 key_put(key);
784error:
785 d_drop(dentry);
786 _leave(" = %d", ret);
787 return ret;
788}
789
790/*
791 * remove a directory from an AFS filesystem
792 */
793static int afs_rmdir(struct inode *dir, struct dentry *dentry)
794{
795 struct afs_vnode *dvnode, *vnode;
796 struct key *key;
797 int ret;
798
799 dvnode = AFS_FS_I(dir);
800
David Howells416351f2007-05-09 02:33:45 -0700801 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700802 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
803
804 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700805 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700806 goto error;
807
808 key = afs_request_key(dvnode->volume->cell);
809 if (IS_ERR(key)) {
810 ret = PTR_ERR(key);
811 goto error;
812 }
813
814 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
815 if (ret < 0)
816 goto rmdir_error;
817
818 if (dentry->d_inode) {
819 vnode = AFS_FS_I(dentry->d_inode);
820 clear_nlink(&vnode->vfs_inode);
821 set_bit(AFS_VNODE_DELETED, &vnode->flags);
822 afs_discard_callback_on_delete(vnode);
823 }
824
825 key_put(key);
826 _leave(" = 0");
827 return 0;
828
829rmdir_error:
830 key_put(key);
831error:
832 _leave(" = %d", ret);
833 return ret;
834}
835
836/*
837 * remove a file from an AFS filesystem
838 */
839static int afs_unlink(struct inode *dir, struct dentry *dentry)
840{
841 struct afs_vnode *dvnode, *vnode;
842 struct key *key;
843 int ret;
844
845 dvnode = AFS_FS_I(dir);
846
David Howells416351f2007-05-09 02:33:45 -0700847 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700848 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
849
850 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700851 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700852 goto error;
853
854 key = afs_request_key(dvnode->volume->cell);
855 if (IS_ERR(key)) {
856 ret = PTR_ERR(key);
857 goto error;
858 }
859
860 if (dentry->d_inode) {
861 vnode = AFS_FS_I(dentry->d_inode);
862
863 /* make sure we have a callback promise on the victim */
864 ret = afs_validate(vnode, key);
865 if (ret < 0)
866 goto error;
867 }
868
869 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
870 if (ret < 0)
871 goto remove_error;
872
873 if (dentry->d_inode) {
874 /* if the file wasn't deleted due to excess hard links, the
875 * fileserver will break the callback promise on the file - if
876 * it had one - before it returns to us, and if it was deleted,
877 * it won't
878 *
879 * however, if we didn't have a callback promise outstanding,
880 * or it was outstanding on a different server, then it won't
881 * break it either...
882 */
883 vnode = AFS_FS_I(dentry->d_inode);
884 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
885 _debug("AFS_VNODE_DELETED");
886 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
887 _debug("AFS_VNODE_CB_BROKEN");
888 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
889 ret = afs_validate(vnode, key);
890 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
891 }
892
893 key_put(key);
894 _leave(" = 0");
895 return 0;
896
897remove_error:
898 key_put(key);
899error:
900 _leave(" = %d", ret);
901 return ret;
902}
903
904/*
905 * create a regular file on an AFS filesystem
906 */
907static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
908 struct nameidata *nd)
909{
910 struct afs_file_status status;
911 struct afs_callback cb;
912 struct afs_server *server;
913 struct afs_vnode *dvnode, *vnode;
914 struct afs_fid fid;
915 struct inode *inode;
916 struct key *key;
917 int ret;
918
919 dvnode = AFS_FS_I(dir);
920
David Howells416351f2007-05-09 02:33:45 -0700921 _enter("{%x:%u},{%s},%o,",
David Howells260a9802007-04-26 15:59:35 -0700922 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
923
924 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700925 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700926 goto error;
927
928 key = afs_request_key(dvnode->volume->cell);
929 if (IS_ERR(key)) {
930 ret = PTR_ERR(key);
931 goto error;
932 }
933
934 mode |= S_IFREG;
935 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
936 mode, &fid, &status, &cb, &server);
937 if (ret < 0)
938 goto create_error;
939
940 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
941 if (IS_ERR(inode)) {
942 /* ENOMEM at a really inconvenient time - just abandon the new
943 * directory on the server */
944 ret = PTR_ERR(inode);
945 goto iget_error;
946 }
947
948 /* apply the status report we've got for the new vnode */
949 vnode = AFS_FS_I(inode);
950 spin_lock(&vnode->lock);
951 vnode->update_cnt++;
952 spin_unlock(&vnode->lock);
953 afs_vnode_finalise_status_update(vnode, server);
954 afs_put_server(server);
955
956 d_instantiate(dentry, inode);
957 if (d_unhashed(dentry)) {
958 _debug("not hashed");
959 d_rehash(dentry);
960 }
961 key_put(key);
962 _leave(" = 0");
963 return 0;
964
965iget_error:
966 afs_put_server(server);
967create_error:
968 key_put(key);
969error:
970 d_drop(dentry);
971 _leave(" = %d", ret);
972 return ret;
973}
974
975/*
976 * create a hard link between files in an AFS filesystem
977 */
978static int afs_link(struct dentry *from, struct inode *dir,
979 struct dentry *dentry)
980{
981 struct afs_vnode *dvnode, *vnode;
982 struct key *key;
983 int ret;
984
985 vnode = AFS_FS_I(from->d_inode);
986 dvnode = AFS_FS_I(dir);
987
David Howells416351f2007-05-09 02:33:45 -0700988 _enter("{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700989 vnode->fid.vid, vnode->fid.vnode,
990 dvnode->fid.vid, dvnode->fid.vnode,
991 dentry->d_name.name);
992
993 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700994 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700995 goto error;
996
997 key = afs_request_key(dvnode->volume->cell);
998 if (IS_ERR(key)) {
999 ret = PTR_ERR(key);
1000 goto error;
1001 }
1002
1003 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1004 if (ret < 0)
1005 goto link_error;
1006
1007 atomic_inc(&vnode->vfs_inode.i_count);
1008 d_instantiate(dentry, &vnode->vfs_inode);
1009 key_put(key);
1010 _leave(" = 0");
1011 return 0;
1012
1013link_error:
1014 key_put(key);
1015error:
1016 d_drop(dentry);
1017 _leave(" = %d", ret);
1018 return ret;
1019}
1020
1021/*
1022 * create a symlink in an AFS filesystem
1023 */
1024static int afs_symlink(struct inode *dir, struct dentry *dentry,
1025 const char *content)
1026{
1027 struct afs_file_status status;
1028 struct afs_server *server;
1029 struct afs_vnode *dvnode, *vnode;
1030 struct afs_fid fid;
1031 struct inode *inode;
1032 struct key *key;
1033 int ret;
1034
1035 dvnode = AFS_FS_I(dir);
1036
David Howells416351f2007-05-09 02:33:45 -07001037 _enter("{%x:%u},{%s},%s",
David Howells260a9802007-04-26 15:59:35 -07001038 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1039 content);
1040
1041 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001042 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001043 goto error;
1044
1045 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001046 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001047 goto error;
1048
1049 key = afs_request_key(dvnode->volume->cell);
1050 if (IS_ERR(key)) {
1051 ret = PTR_ERR(key);
1052 goto error;
1053 }
1054
1055 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1056 &fid, &status, &server);
1057 if (ret < 0)
1058 goto create_error;
1059
1060 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1061 if (IS_ERR(inode)) {
1062 /* ENOMEM at a really inconvenient time - just abandon the new
1063 * directory on the server */
1064 ret = PTR_ERR(inode);
1065 goto iget_error;
1066 }
1067
1068 /* apply the status report we've got for the new vnode */
1069 vnode = AFS_FS_I(inode);
1070 spin_lock(&vnode->lock);
1071 vnode->update_cnt++;
1072 spin_unlock(&vnode->lock);
1073 afs_vnode_finalise_status_update(vnode, server);
1074 afs_put_server(server);
1075
1076 d_instantiate(dentry, inode);
1077 if (d_unhashed(dentry)) {
1078 _debug("not hashed");
1079 d_rehash(dentry);
1080 }
1081 key_put(key);
1082 _leave(" = 0");
1083 return 0;
1084
1085iget_error:
1086 afs_put_server(server);
1087create_error:
1088 key_put(key);
1089error:
1090 d_drop(dentry);
1091 _leave(" = %d", ret);
1092 return ret;
1093}
1094
1095/*
1096 * rename a file in an AFS filesystem and/or move it between directories
1097 */
1098static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1099 struct inode *new_dir, struct dentry *new_dentry)
1100{
1101 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1102 struct key *key;
1103 int ret;
1104
1105 vnode = AFS_FS_I(old_dentry->d_inode);
1106 orig_dvnode = AFS_FS_I(old_dir);
1107 new_dvnode = AFS_FS_I(new_dir);
1108
David Howells416351f2007-05-09 02:33:45 -07001109 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001110 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1111 vnode->fid.vid, vnode->fid.vnode,
1112 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1113 new_dentry->d_name.name);
1114
1115 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -07001116 if (new_dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -07001117 goto error;
1118
1119 key = afs_request_key(orig_dvnode->volume->cell);
1120 if (IS_ERR(key)) {
1121 ret = PTR_ERR(key);
1122 goto error;
1123 }
1124
1125 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1126 old_dentry->d_name.name,
1127 new_dentry->d_name.name);
1128 if (ret < 0)
1129 goto rename_error;
1130 key_put(key);
1131 _leave(" = 0");
1132 return 0;
1133
1134rename_error:
1135 key_put(key);
1136error:
1137 d_drop(new_dentry);
1138 _leave(" = %d", ret);
1139 return ret;
1140}