blob: 646337dc5201e702227309cc17db99f96af99173 [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,
Al Viro00cd8dd2012-06-10 17:13:09 -040023 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int afs_dir_open(struct inode *inode, struct file *file);
Al Viro1bbae9f2013-05-22 16:31:14 -040025static int afs_readdir(struct file *file, struct dir_context *ctx);
Al Viro0b728e12012-06-10 16:03:43 -040026static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
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);
Al Viro4acdaf22011-07-26 01:42:34 -040031static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040032 bool excl);
Al Viro18bb1db2011-07-26 01:41:39 -040033static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
David Howells260a9802007-04-26 15:59:35 -070034static 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,
Al Viro1bbae9f2013-05-22 16:31:14 -040046 .iterate = 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,
David Howellsd18610b2011-01-14 19:04:05 +000069 .d_automount = afs_d_automount,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
72#define AFS_DIR_HASHTBL_SIZE 128
73#define AFS_DIR_DIRENT_SIZE 32
74#define AFS_DIRENT_PER_BLOCK 64
75
76union afs_dirent {
77 struct {
78 uint8_t valid;
79 uint8_t unused[1];
80 __be16 hash_next;
81 __be32 vnode;
82 __be32 unique;
83 uint8_t name[16];
84 uint8_t overflow[4]; /* if any char of the name (inc
85 * NUL) reaches here, consume
86 * the next dirent too */
87 } u;
88 uint8_t extended_name[32];
89};
90
91/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
92struct afs_dir_pagehdr {
93 __be16 npages;
94 __be16 magic;
95#define AFS_DIR_MAGIC htons(1234)
96 uint8_t nentries;
97 uint8_t bitmap[8];
98 uint8_t pad[19];
99};
100
101/* directory block layout */
102union afs_dir_block {
103
104 struct afs_dir_pagehdr pagehdr;
105
106 struct {
107 struct afs_dir_pagehdr pagehdr;
108 uint8_t alloc_ctrs[128];
109 /* dir hash table */
110 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
111 } hdr;
112
113 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
114};
115
116/* layout on a linux VM page */
117struct afs_dir_page {
118 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
119};
120
David Howells260a9802007-04-26 15:59:35 -0700121struct afs_lookup_cookie {
Al Viro1bbae9f2013-05-22 16:31:14 -0400122 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct afs_fid fid;
Al Viro1bbae9f2013-05-22 16:31:14 -0400124 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int found;
126};
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * check that a directory page is valid
130 */
131static inline void afs_dir_check_page(struct inode *dir, struct page *page)
132{
133 struct afs_dir_page *dbuf;
134 loff_t latter;
135 int tmp, qty;
136
137#if 0
138 /* check the page count */
139 qty = desc.size / sizeof(dbuf->blocks[0]);
140 if (qty == 0)
141 goto error;
142
David Howells08e0e7c2007-04-26 15:55:03 -0700143 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700145 __func__, dir->i_ino, qty,
David Howells08e0e7c2007-04-26 15:55:03 -0700146 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto error;
148 }
149#endif
150
151 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800152 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (latter >= PAGE_SIZE)
154 qty = PAGE_SIZE;
155 else
156 qty = latter;
157 qty /= sizeof(union afs_dir_block);
158
159 /* check them */
160 dbuf = page_address(page);
161 for (tmp = 0; tmp < qty; tmp++) {
162 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
163 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700164 __func__, dir->i_ino, tmp, qty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ntohs(dbuf->blocks[tmp].pagehdr.magic));
166 goto error;
167 }
168 }
169
170 SetPageChecked(page);
171 return;
172
David Howellsec268152007-04-26 15:49:28 -0700173error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 SetPageChecked(page);
175 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * discard a page cached in the pagecache
180 */
181static inline void afs_dir_put_page(struct page *page)
182{
183 kunmap(page);
184 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * get a page into the pagecache
189 */
David Howells00d3b7a2007-04-26 15:57:07 -0700190static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
191 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 _enter("{%lu},%lu", dir->i_ino, index);
195
Al Virof6d335c2010-05-21 15:27:09 +0100196 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!PageChecked(page))
200 afs_dir_check_page(dir, page);
201 if (PageError(page))
202 goto fail;
203 }
204 return page;
205
David Howellsec268152007-04-26 15:49:28 -0700206fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700208 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * open an AFS directory file
214 */
215static int afs_dir_open(struct inode *inode, struct file *file)
216{
217 _enter("{%lu}", inode->i_ino);
218
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700219 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
220 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
David Howells08e0e7c2007-04-26 15:55:03 -0700222 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -ENOENT;
224
David Howells00d3b7a2007-04-26 15:57:07 -0700225 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * deal with one block in an AFS directory
230 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400231static int afs_dir_iterate_block(struct dir_context *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 union afs_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400233 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 union afs_dirent *dire;
236 unsigned offset, next, curr;
237 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400238 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Al Viro1bbae9f2013-05-22 16:31:14 -0400240 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Al Viro1bbae9f2013-05-22 16:31:14 -0400242 curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
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)
Al Viro1bbae9f2013-05-22 16:31:14 -0400257 ctx->pos = blkoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 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 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400303 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ntohl(dire->u.vnode),
Al Viro1bbae9f2013-05-22 16:31:14 -0400305 ctx->actor == afs_lookup_filldir ?
306 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 _leave(" = 0 [full]");
308 return 0;
309 }
310
Al Viro1bbae9f2013-05-22 16:31:14 -0400311 ctx->pos = blkoff + next * sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 _leave(" = 1 [more]");
315 return 1;
David Howellsec268152007-04-26 15:49:28 -0700316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
David Howells08e0e7c2007-04-26 15:55:03 -0700319 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400321static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
322 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David Howells08e0e7c2007-04-26 15:55:03 -0700324 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct afs_dir_page *dbuf;
326 struct page *page;
327 unsigned blkoff, limit;
328 int ret;
329
Al Viro1bbae9f2013-05-22 16:31:14 -0400330 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells08e0e7c2007-04-26 15:55:03 -0700332 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 _leave(" = -ESTALE");
334 return -ESTALE;
335 }
336
337 /* round the file position up to the next entry boundary */
Al Viro1bbae9f2013-05-22 16:31:14 -0400338 ctx->pos += sizeof(union afs_dirent) - 1;
339 ctx->pos &= ~(sizeof(union afs_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* walk through the blocks in sequence */
342 ret = 0;
Al Viro1bbae9f2013-05-22 16:31:14 -0400343 while (ctx->pos < dir->i_size) {
344 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700347 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (IS_ERR(page)) {
349 ret = PTR_ERR(page);
350 break;
351 }
352
353 limit = blkoff & ~(PAGE_SIZE - 1);
354
355 dbuf = page_address(page);
356
357 /* deal with the individual blocks stashed on this page */
358 do {
359 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
360 sizeof(union afs_dir_block)];
Al Viro1bbae9f2013-05-22 16:31:14 -0400361 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (ret != 1) {
363 afs_dir_put_page(page);
364 goto out;
365 }
366
367 blkoff += sizeof(union afs_dir_block);
368
Al Viro1bbae9f2013-05-22 16:31:14 -0400369 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 afs_dir_put_page(page);
372 ret = 0;
373 }
374
David Howellsec268152007-04-26 15:49:28 -0700375out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 _leave(" = %d", ret);
377 return ret;
David Howellsec268152007-04-26 15:49:28 -0700378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * read an AFS directory
382 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400383static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Al Viro1bbae9f2013-05-22 16:31:14 -0400385 return afs_dir_iterate(file_inode(file),
386 ctx, file->private_data);
David Howellsec268152007-04-26 15:49:28 -0700387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * search the directory for a name
391 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
392 * uniquifier through dtype
393 */
David Howells260a9802007-04-26 15:59:35 -0700394static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
395 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
David Howells260a9802007-04-26 15:59:35 -0700397 struct afs_lookup_cookie *cookie = _cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Al Viro1bbae9f2013-05-22 16:31:14 -0400399 _enter("{%s,%u},%s,%u,,%llu,%u",
400 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700401 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells08e0e7c2007-04-26 15:55:03 -0700403 /* insanity checks first */
404 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
405 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
406
Al Viro1bbae9f2013-05-22 16:31:14 -0400407 if (cookie->name.len != nlen ||
408 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 _leave(" = 0 [no]");
410 return 0;
411 }
412
413 cookie->fid.vnode = ino;
414 cookie->fid.unique = dtype;
415 cookie->found = 1;
416
417 _leave(" = -1 [found]");
418 return -1;
David Howellsec268152007-04-26 15:49:28 -0700419}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
David Howells08e0e7c2007-04-26 15:55:03 -0700422 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700423 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
David Howells08e0e7c2007-04-26 15:55:03 -0700425static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700426 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Al Viro1bbae9f2013-05-22 16:31:14 -0400428 struct afs_super_info *as = dir->i_sb->s_fs_info;
429 struct afs_lookup_cookie cookie = {
430 .ctx.actor = afs_lookup_filldir,
431 .name = dentry->d_name,
432 .fid.vid = as->volume->vid
433 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int ret;
435
David Howells08e0e7c2007-04-26 15:55:03 -0700436 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400439 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700441 _leave(" = %d [iter]", ret);
442 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 ret = -ENOENT;
446 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700447 _leave(" = -ENOENT [not found]");
448 return -ENOENT;
449 }
450
451 *fid = cookie.fid;
452 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
453 return 0;
454}
455
456/*
wangleibec5eb62010-08-11 09:38:04 +0100457 * Try to auto mount the mountpoint with pseudo directory, if the autocell
458 * operation is setted.
459 */
460static struct inode *afs_try_auto_mntpt(
461 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
462 struct afs_fid *fid)
463{
464 const char *devname = dentry->d_name.name;
465 struct afs_vnode *vnode = AFS_FS_I(dir);
466 struct inode *inode;
467
468 _enter("%d, %p{%s}, {%x:%u}, %p",
469 ret, dentry, devname, vnode->fid.vid, vnode->fid.vnode, key);
470
471 if (ret != -ENOENT ||
472 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
473 goto out;
474
475 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
476 if (IS_ERR(inode)) {
477 ret = PTR_ERR(inode);
478 goto out;
479 }
480
481 *fid = AFS_FS_I(inode)->fid;
482 _leave("= %p", inode);
483 return inode;
484
485out:
486 _leave("= %d", ret);
487 return ERR_PTR(ret);
488}
489
490/*
David Howells08e0e7c2007-04-26 15:55:03 -0700491 * look up an entry in a directory
492 */
David Howells260a9802007-04-26 15:59:35 -0700493static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400494 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700495{
496 struct afs_vnode *vnode;
497 struct afs_fid fid;
498 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700499 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700500 int ret;
501
David Howells260a9802007-04-26 15:59:35 -0700502 vnode = AFS_FS_I(dir);
503
David Howells416351f2007-05-09 02:33:45 -0700504 _enter("{%x:%u},%p{%s},",
David Howells260a9802007-04-26 15:59:35 -0700505 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
506
507 ASSERTCMP(dentry->d_inode, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700508
David Howells45222b92007-05-10 22:22:20 -0700509 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700510 _leave(" = -ENAMETOOLONG");
511 return ERR_PTR(-ENAMETOOLONG);
512 }
513
David Howells08e0e7c2007-04-26 15:55:03 -0700514 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
515 _leave(" = -ESTALE");
516 return ERR_PTR(-ESTALE);
517 }
518
David Howells00d3b7a2007-04-26 15:57:07 -0700519 key = afs_request_key(vnode->volume->cell);
520 if (IS_ERR(key)) {
521 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800522 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700523 }
524
David Howells260a9802007-04-26 15:59:35 -0700525 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700526 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700527 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700528 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return ERR_PTR(ret);
530 }
531
David Howells260a9802007-04-26 15:59:35 -0700532 ret = afs_do_lookup(dir, dentry, &fid, key);
533 if (ret < 0) {
wangleibec5eb62010-08-11 09:38:04 +0100534 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
535 if (!IS_ERR(inode)) {
536 key_put(key);
537 goto success;
538 }
539
540 ret = PTR_ERR(inode);
David Howells260a9802007-04-26 15:59:35 -0700541 key_put(key);
542 if (ret == -ENOENT) {
543 d_add(dentry, NULL);
544 _leave(" = NULL [negative]");
545 return NULL;
546 }
547 _leave(" = %d [do]", ret);
548 return ERR_PTR(ret);
549 }
550 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700553 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700554 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700555 if (IS_ERR(inode)) {
556 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800557 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
wangleibec5eb62010-08-11 09:38:04 +0100560success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 d_add(dentry, inode);
David Howellsd6e43f72011-06-14 00:45:44 +0100562 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
David Howells08e0e7c2007-04-26 15:55:03 -0700563 fid.vnode,
564 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 dentry->d_inode->i_ino,
David Howellsd6e43f72011-06-14 00:45:44 +0100566 dentry->d_inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700569}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
572 * check that a dentry lookup hit has found a valid entry
573 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
574 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Al Viro0b728e12012-06-10 16:03:43 -0400576static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
David Howells260a9802007-04-26 15:59:35 -0700578 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100579 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700581 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700582 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int ret;
584
Al Viro0b728e12012-06-10 16:03:43 -0400585 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100586 return -ECHILD;
587
David Howells08e0e7c2007-04-26 15:55:03 -0700588 vnode = AFS_FS_I(dentry->d_inode);
589
David Howells260a9802007-04-26 15:59:35 -0700590 if (dentry->d_inode)
591 _enter("{v={%x:%u} n=%s fl=%lx},",
592 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
593 vnode->flags);
594 else
595 _enter("{neg n=%s}", dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
David Howells260a9802007-04-26 15:59:35 -0700597 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700598 if (IS_ERR(key))
599 key = NULL;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700602 parent = dget_parent(dentry);
David Howells260a9802007-04-26 15:59:35 -0700603 if (!parent->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto out_bad;
605
David Howells260a9802007-04-26 15:59:35 -0700606 dir = AFS_FS_I(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
David Howells260a9802007-04-26 15:59:35 -0700608 /* validate the parent directory */
609 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
610 afs_validate(dir, key);
611
612 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 _debug("%s: parent dir deleted", dentry->d_name.name);
614 goto out_bad;
615 }
616
David Howells260a9802007-04-26 15:59:35 -0700617 dir_version = (void *) (unsigned long) dir->status.data_version;
618 if (dentry->d_fsdata == dir_version)
619 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
David Howells260a9802007-04-26 15:59:35 -0700621 _debug("dir modified");
622
623 /* search the directory for this vnode */
624 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
625 switch (ret) {
626 case 0:
627 /* the filename maps to something */
628 if (!dentry->d_inode)
629 goto out_bad;
630 if (is_bad_inode(dentry->d_inode)) {
631 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
632 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto out_bad;
634 }
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* if the vnode ID has changed, then the dirent points to a
637 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700638 if (fid.vnode != vnode->fid.vnode) {
639 _debug("%s: dirent changed [%u != %u]",
640 dentry->d_name.name, fid.vnode,
641 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto not_found;
643 }
644
645 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700646 * been deleted and replaced, and the original vnode ID has
647 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700648 if (fid.unique != vnode->fid.unique) {
David Howellsd6e43f72011-06-14 00:45:44 +0100649 _debug("%s: file deleted (uq %u -> %u I:%u)",
David Howells08e0e7c2007-04-26 15:55:03 -0700650 dentry->d_name.name, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500651 vnode->fid.unique,
David Howellsd6e43f72011-06-14 00:45:44 +0100652 dentry->d_inode->i_generation);
David Howells08e0e7c2007-04-26 15:55:03 -0700653 spin_lock(&vnode->lock);
654 set_bit(AFS_VNODE_DELETED, &vnode->flags);
655 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700656 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
David Howells260a9802007-04-26 15:59:35 -0700658 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
David Howells260a9802007-04-26 15:59:35 -0700660 case -ENOENT:
661 /* the filename is unknown */
662 _debug("%s: dirent not found", dentry->d_name.name);
663 if (dentry->d_inode)
664 goto not_found;
665 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700666
David Howells260a9802007-04-26 15:59:35 -0700667 default:
668 _debug("failed to iterate dir %s: %d",
669 parent->d_name.name, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700670 goto out_bad;
671 }
672
David Howellsec268152007-04-26 15:49:28 -0700673out_valid:
David Howells260a9802007-04-26 15:59:35 -0700674 dentry->d_fsdata = dir_version;
675out_skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700677 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 _leave(" = 1 [valid]");
679 return 1;
680
681 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700682not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 spin_lock(&dentry->d_lock);
684 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
685 spin_unlock(&dentry->d_lock);
686
David Howellsec268152007-04-26 15:49:28 -0700687out_bad:
Miklos Szerediba812382013-09-05 11:44:38 +0200688 /* don't unhash if we have submounts */
689 if (check_submounts_and_drop(dentry) != 0)
690 goto out_skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700693 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700695 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 _leave(" = 0 [bad]");
698 return 0;
David Howellsec268152007-04-26 15:49:28 -0700699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
703 * sleep)
704 * - called from dput() when d_count is going to 0.
705 * - return 1 to request dentry be unhashed, 0 otherwise
706 */
Nick Pigginfe15ce42011-01-07 17:49:23 +1100707static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 _enter("%s", dentry->d_name.name);
710
711 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
712 goto zap;
713
David Howells08e0e7c2007-04-26 15:55:03 -0700714 if (dentry->d_inode &&
wangleibec5eb62010-08-11 09:38:04 +0100715 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags) ||
716 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(dentry->d_inode)->flags)))
717 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 _leave(" = 0 [keep]");
720 return 0;
721
David Howellsec268152007-04-26 15:49:28 -0700722zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 _leave(" = 1 [zap]");
724 return 1;
David Howellsec268152007-04-26 15:49:28 -0700725}
David Howells260a9802007-04-26 15:59:35 -0700726
727/*
728 * handle dentry release
729 */
730static void afs_d_release(struct dentry *dentry)
731{
732 _enter("%s", dentry->d_name.name);
733}
734
735/*
736 * create a directory on an AFS filesystem
737 */
Al Viro18bb1db2011-07-26 01:41:39 -0400738static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -0700739{
740 struct afs_file_status status;
741 struct afs_callback cb;
742 struct afs_server *server;
743 struct afs_vnode *dvnode, *vnode;
744 struct afs_fid fid;
745 struct inode *inode;
746 struct key *key;
747 int ret;
748
749 dvnode = AFS_FS_I(dir);
750
Al Viro18bb1db2011-07-26 01:41:39 -0400751 _enter("{%x:%u},{%s},%ho",
David Howells260a9802007-04-26 15:59:35 -0700752 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
753
David Howells260a9802007-04-26 15:59:35 -0700754 key = afs_request_key(dvnode->volume->cell);
755 if (IS_ERR(key)) {
756 ret = PTR_ERR(key);
757 goto error;
758 }
759
760 mode |= S_IFDIR;
761 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
762 mode, &fid, &status, &cb, &server);
763 if (ret < 0)
764 goto mkdir_error;
765
766 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
767 if (IS_ERR(inode)) {
768 /* ENOMEM at a really inconvenient time - just abandon the new
769 * directory on the server */
770 ret = PTR_ERR(inode);
771 goto iget_error;
772 }
773
774 /* apply the status report we've got for the new vnode */
775 vnode = AFS_FS_I(inode);
776 spin_lock(&vnode->lock);
777 vnode->update_cnt++;
778 spin_unlock(&vnode->lock);
779 afs_vnode_finalise_status_update(vnode, server);
780 afs_put_server(server);
781
782 d_instantiate(dentry, inode);
783 if (d_unhashed(dentry)) {
784 _debug("not hashed");
785 d_rehash(dentry);
786 }
787 key_put(key);
788 _leave(" = 0");
789 return 0;
790
791iget_error:
792 afs_put_server(server);
793mkdir_error:
794 key_put(key);
795error:
796 d_drop(dentry);
797 _leave(" = %d", ret);
798 return ret;
799}
800
801/*
802 * remove a directory from an AFS filesystem
803 */
804static int afs_rmdir(struct inode *dir, struct dentry *dentry)
805{
806 struct afs_vnode *dvnode, *vnode;
807 struct key *key;
808 int ret;
809
810 dvnode = AFS_FS_I(dir);
811
David Howells416351f2007-05-09 02:33:45 -0700812 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700813 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
814
David Howells260a9802007-04-26 15:59:35 -0700815 key = afs_request_key(dvnode->volume->cell);
816 if (IS_ERR(key)) {
817 ret = PTR_ERR(key);
818 goto error;
819 }
820
821 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
822 if (ret < 0)
823 goto rmdir_error;
824
825 if (dentry->d_inode) {
826 vnode = AFS_FS_I(dentry->d_inode);
827 clear_nlink(&vnode->vfs_inode);
828 set_bit(AFS_VNODE_DELETED, &vnode->flags);
829 afs_discard_callback_on_delete(vnode);
830 }
831
832 key_put(key);
833 _leave(" = 0");
834 return 0;
835
836rmdir_error:
837 key_put(key);
838error:
839 _leave(" = %d", ret);
840 return ret;
841}
842
843/*
844 * remove a file from an AFS filesystem
845 */
846static int afs_unlink(struct inode *dir, struct dentry *dentry)
847{
848 struct afs_vnode *dvnode, *vnode;
849 struct key *key;
850 int ret;
851
852 dvnode = AFS_FS_I(dir);
853
David Howells416351f2007-05-09 02:33:45 -0700854 _enter("{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700855 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
856
857 ret = -ENAMETOOLONG;
David Howells45222b92007-05-10 22:22:20 -0700858 if (dentry->d_name.len >= AFSNAMEMAX)
David Howells260a9802007-04-26 15:59:35 -0700859 goto error;
860
861 key = afs_request_key(dvnode->volume->cell);
862 if (IS_ERR(key)) {
863 ret = PTR_ERR(key);
864 goto error;
865 }
866
867 if (dentry->d_inode) {
868 vnode = AFS_FS_I(dentry->d_inode);
869
870 /* make sure we have a callback promise on the victim */
871 ret = afs_validate(vnode, key);
872 if (ret < 0)
873 goto error;
874 }
875
876 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
877 if (ret < 0)
878 goto remove_error;
879
880 if (dentry->d_inode) {
881 /* if the file wasn't deleted due to excess hard links, the
882 * fileserver will break the callback promise on the file - if
883 * it had one - before it returns to us, and if it was deleted,
884 * it won't
885 *
886 * however, if we didn't have a callback promise outstanding,
887 * or it was outstanding on a different server, then it won't
888 * break it either...
889 */
890 vnode = AFS_FS_I(dentry->d_inode);
891 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
892 _debug("AFS_VNODE_DELETED");
893 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
894 _debug("AFS_VNODE_CB_BROKEN");
895 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
896 ret = afs_validate(vnode, key);
897 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
898 }
899
900 key_put(key);
901 _leave(" = 0");
902 return 0;
903
904remove_error:
905 key_put(key);
906error:
907 _leave(" = %d", ret);
908 return ret;
909}
910
911/*
912 * create a regular file on an AFS filesystem
913 */
Al Viro4acdaf22011-07-26 01:42:34 -0400914static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400915 bool excl)
David Howells260a9802007-04-26 15:59:35 -0700916{
917 struct afs_file_status status;
918 struct afs_callback cb;
919 struct afs_server *server;
920 struct afs_vnode *dvnode, *vnode;
921 struct afs_fid fid;
922 struct inode *inode;
923 struct key *key;
924 int ret;
925
926 dvnode = AFS_FS_I(dir);
927
Al Viro4acdaf22011-07-26 01:42:34 -0400928 _enter("{%x:%u},{%s},%ho,",
David Howells260a9802007-04-26 15:59:35 -0700929 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
930
David Howells260a9802007-04-26 15:59:35 -0700931 key = afs_request_key(dvnode->volume->cell);
932 if (IS_ERR(key)) {
933 ret = PTR_ERR(key);
934 goto error;
935 }
936
937 mode |= S_IFREG;
938 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
939 mode, &fid, &status, &cb, &server);
940 if (ret < 0)
941 goto create_error;
942
943 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
944 if (IS_ERR(inode)) {
945 /* ENOMEM at a really inconvenient time - just abandon the new
946 * directory on the server */
947 ret = PTR_ERR(inode);
948 goto iget_error;
949 }
950
951 /* apply the status report we've got for the new vnode */
952 vnode = AFS_FS_I(inode);
953 spin_lock(&vnode->lock);
954 vnode->update_cnt++;
955 spin_unlock(&vnode->lock);
956 afs_vnode_finalise_status_update(vnode, server);
957 afs_put_server(server);
958
959 d_instantiate(dentry, inode);
960 if (d_unhashed(dentry)) {
961 _debug("not hashed");
962 d_rehash(dentry);
963 }
964 key_put(key);
965 _leave(" = 0");
966 return 0;
967
968iget_error:
969 afs_put_server(server);
970create_error:
971 key_put(key);
972error:
973 d_drop(dentry);
974 _leave(" = %d", ret);
975 return ret;
976}
977
978/*
979 * create a hard link between files in an AFS filesystem
980 */
981static int afs_link(struct dentry *from, struct inode *dir,
982 struct dentry *dentry)
983{
984 struct afs_vnode *dvnode, *vnode;
985 struct key *key;
986 int ret;
987
988 vnode = AFS_FS_I(from->d_inode);
989 dvnode = AFS_FS_I(dir);
990
David Howells416351f2007-05-09 02:33:45 -0700991 _enter("{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -0700992 vnode->fid.vid, vnode->fid.vnode,
993 dvnode->fid.vid, dvnode->fid.vnode,
994 dentry->d_name.name);
995
David Howells260a9802007-04-26 15:59:35 -0700996 key = afs_request_key(dvnode->volume->cell);
997 if (IS_ERR(key)) {
998 ret = PTR_ERR(key);
999 goto error;
1000 }
1001
1002 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1003 if (ret < 0)
1004 goto link_error;
1005
Al Viro7de9c6ee2010-10-23 11:11:40 -04001006 ihold(&vnode->vfs_inode);
David Howells260a9802007-04-26 15:59:35 -07001007 d_instantiate(dentry, &vnode->vfs_inode);
1008 key_put(key);
1009 _leave(" = 0");
1010 return 0;
1011
1012link_error:
1013 key_put(key);
1014error:
1015 d_drop(dentry);
1016 _leave(" = %d", ret);
1017 return ret;
1018}
1019
1020/*
1021 * create a symlink in an AFS filesystem
1022 */
1023static int afs_symlink(struct inode *dir, struct dentry *dentry,
1024 const char *content)
1025{
1026 struct afs_file_status status;
1027 struct afs_server *server;
1028 struct afs_vnode *dvnode, *vnode;
1029 struct afs_fid fid;
1030 struct inode *inode;
1031 struct key *key;
1032 int ret;
1033
1034 dvnode = AFS_FS_I(dir);
1035
David Howells416351f2007-05-09 02:33:45 -07001036 _enter("{%x:%u},{%s},%s",
David Howells260a9802007-04-26 15:59:35 -07001037 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1038 content);
1039
David Howells260a9802007-04-26 15:59:35 -07001040 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001041 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001042 goto error;
1043
1044 key = afs_request_key(dvnode->volume->cell);
1045 if (IS_ERR(key)) {
1046 ret = PTR_ERR(key);
1047 goto error;
1048 }
1049
1050 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1051 &fid, &status, &server);
1052 if (ret < 0)
1053 goto create_error;
1054
1055 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1056 if (IS_ERR(inode)) {
1057 /* ENOMEM at a really inconvenient time - just abandon the new
1058 * directory on the server */
1059 ret = PTR_ERR(inode);
1060 goto iget_error;
1061 }
1062
1063 /* apply the status report we've got for the new vnode */
1064 vnode = AFS_FS_I(inode);
1065 spin_lock(&vnode->lock);
1066 vnode->update_cnt++;
1067 spin_unlock(&vnode->lock);
1068 afs_vnode_finalise_status_update(vnode, server);
1069 afs_put_server(server);
1070
1071 d_instantiate(dentry, inode);
1072 if (d_unhashed(dentry)) {
1073 _debug("not hashed");
1074 d_rehash(dentry);
1075 }
1076 key_put(key);
1077 _leave(" = 0");
1078 return 0;
1079
1080iget_error:
1081 afs_put_server(server);
1082create_error:
1083 key_put(key);
1084error:
1085 d_drop(dentry);
1086 _leave(" = %d", ret);
1087 return ret;
1088}
1089
1090/*
1091 * rename a file in an AFS filesystem and/or move it between directories
1092 */
1093static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1094 struct inode *new_dir, struct dentry *new_dentry)
1095{
1096 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1097 struct key *key;
1098 int ret;
1099
1100 vnode = AFS_FS_I(old_dentry->d_inode);
1101 orig_dvnode = AFS_FS_I(old_dir);
1102 new_dvnode = AFS_FS_I(new_dir);
1103
David Howells416351f2007-05-09 02:33:45 -07001104 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
David Howells260a9802007-04-26 15:59:35 -07001105 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1106 vnode->fid.vid, vnode->fid.vnode,
1107 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1108 new_dentry->d_name.name);
1109
David Howells260a9802007-04-26 15:59:35 -07001110 key = afs_request_key(orig_dvnode->volume->cell);
1111 if (IS_ERR(key)) {
1112 ret = PTR_ERR(key);
1113 goto error;
1114 }
1115
1116 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1117 old_dentry->d_name.name,
1118 new_dentry->d_name.name);
1119 if (ret < 0)
1120 goto rename_error;
1121 key_put(key);
1122 _leave(" = 0");
1123 return 0;
1124
1125rename_error:
1126 key_put(key);
1127error:
1128 d_drop(new_dentry);
1129 _leave(" = %d", ret);
1130 return ret;
1131}