blob: 949f960337f5b30a880d5f6c547dca6eccfc748d [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);
Miklos Szerediac7576f2014-10-30 17:37:34 +010029static int afs_lookup_filldir(struct dir_context *ctx, 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,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +020041 struct inode *new_dir, struct dentry *new_dentry,
42 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080044const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070046 .release = afs_release,
Al Viro29884ef2016-05-10 14:27:44 -040047 .iterate_shared = afs_readdir,
David Howellse8d6c552007-07-15 23:40:12 -070048 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020049 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Arjan van de Ven754661f2007-02-12 00:55:38 -080052const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070053 .create = afs_create,
54 .lookup = afs_lookup,
55 .link = afs_link,
56 .unlink = afs_unlink,
57 .symlink = afs_symlink,
58 .mkdir = afs_mkdir,
59 .rmdir = afs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020060 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070061 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070062 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070063 .setattr = afs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Al Virod61dcce2011-01-12 20:04:20 -050066const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .d_revalidate = afs_d_revalidate,
68 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070069 .d_release = afs_d_release,
David Howellsd18610b2011-01-14 19:04:05 +000070 .d_automount = afs_d_automount,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73#define AFS_DIR_HASHTBL_SIZE 128
74#define AFS_DIR_DIRENT_SIZE 32
75#define AFS_DIRENT_PER_BLOCK 64
76
77union afs_dirent {
78 struct {
79 uint8_t valid;
80 uint8_t unused[1];
81 __be16 hash_next;
82 __be32 vnode;
83 __be32 unique;
84 uint8_t name[16];
85 uint8_t overflow[4]; /* if any char of the name (inc
86 * NUL) reaches here, consume
87 * the next dirent too */
88 } u;
89 uint8_t extended_name[32];
90};
91
92/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
93struct afs_dir_pagehdr {
94 __be16 npages;
95 __be16 magic;
96#define AFS_DIR_MAGIC htons(1234)
97 uint8_t nentries;
98 uint8_t bitmap[8];
99 uint8_t pad[19];
100};
101
102/* directory block layout */
103union afs_dir_block {
104
105 struct afs_dir_pagehdr pagehdr;
106
107 struct {
108 struct afs_dir_pagehdr pagehdr;
109 uint8_t alloc_ctrs[128];
110 /* dir hash table */
111 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
112 } hdr;
113
114 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
115};
116
117/* layout on a linux VM page */
118struct afs_dir_page {
119 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
120};
121
David Howells260a9802007-04-26 15:59:35 -0700122struct afs_lookup_cookie {
Al Viro1bbae9f2013-05-22 16:31:14 -0400123 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct afs_fid fid;
Al Viro1bbae9f2013-05-22 16:31:14 -0400125 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int found;
127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
130 * check that a directory page is valid
131 */
Al Virobe5b82d2016-04-22 15:06:44 -0400132static inline bool afs_dir_check_page(struct inode *dir, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct afs_dir_page *dbuf;
135 loff_t latter;
136 int tmp, qty;
137
138#if 0
139 /* check the page count */
140 qty = desc.size / sizeof(dbuf->blocks[0]);
141 if (qty == 0)
142 goto error;
143
David Howells08e0e7c2007-04-26 15:55:03 -0700144 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700146 __func__, dir->i_ino, qty,
David Howells08e0e7c2007-04-26 15:55:03 -0700147 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto error;
149 }
150#endif
151
152 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800153 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (latter >= PAGE_SIZE)
155 qty = PAGE_SIZE;
156 else
157 qty = latter;
158 qty /= sizeof(union afs_dir_block);
159
160 /* check them */
161 dbuf = page_address(page);
162 for (tmp = 0; tmp < qty; tmp++) {
163 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
164 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700165 __func__, dir->i_ino, tmp, qty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 ntohs(dbuf->blocks[tmp].pagehdr.magic));
167 goto error;
168 }
169 }
170
171 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400172 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Howellsec268152007-04-26 15:49:28 -0700174error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400176 return false;
David Howellsec268152007-04-26 15:49:28 -0700177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * discard a page cached in the pagecache
181 */
182static inline void afs_dir_put_page(struct page *page)
183{
184 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300185 put_page(page);
David Howellsec268152007-04-26 15:49:28 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * get a page into the pagecache
190 */
David Howells00d3b7a2007-04-26 15:57:07 -0700191static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
192 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 _enter("{%lu},%lu", dir->i_ino, index);
196
Al Virof6d335c2010-05-21 15:27:09 +0100197 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400200 if (unlikely(!PageChecked(page))) {
201 if (PageError(page) || !afs_dir_check_page(dir, page))
202 goto fail;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
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 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400232static int afs_dir_iterate_block(struct dir_context *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 union afs_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400234 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 union afs_dirent *dire;
237 unsigned offset, next, curr;
238 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400239 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Viro1bbae9f2013-05-22 16:31:14 -0400241 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Al Viro1bbae9f2013-05-22 16:31:14 -0400243 curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
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)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800255 _debug("ENT[%zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 blkoff / sizeof(union afs_dir_block), offset);
257 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400258 ctx->pos = blkoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800269 _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) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800277 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800279 " (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)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800286 _debug("ENT[%zu.%u]:"
287 " %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
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800293 _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 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400304 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ntohl(dire->u.vnode),
Al Viro1bbae9f2013-05-22 16:31:14 -0400306 ctx->actor == afs_lookup_filldir ?
307 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 _leave(" = 0 [full]");
309 return 0;
310 }
311
Al Viro1bbae9f2013-05-22 16:31:14 -0400312 ctx->pos = blkoff + next * sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 _leave(" = 1 [more]");
316 return 1;
David Howellsec268152007-04-26 15:49:28 -0700317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
David Howells08e0e7c2007-04-26 15:55:03 -0700320 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400322static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
323 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Howells08e0e7c2007-04-26 15:55:03 -0700325 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct afs_dir_page *dbuf;
327 struct page *page;
328 unsigned blkoff, limit;
329 int ret;
330
Al Viro1bbae9f2013-05-22 16:31:14 -0400331 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David Howells08e0e7c2007-04-26 15:55:03 -0700333 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 _leave(" = -ESTALE");
335 return -ESTALE;
336 }
337
338 /* round the file position up to the next entry boundary */
Al Viro1bbae9f2013-05-22 16:31:14 -0400339 ctx->pos += sizeof(union afs_dirent) - 1;
340 ctx->pos &= ~(sizeof(union afs_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* walk through the blocks in sequence */
343 ret = 0;
Al Viro1bbae9f2013-05-22 16:31:14 -0400344 while (ctx->pos < dir->i_size) {
345 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700348 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (IS_ERR(page)) {
350 ret = PTR_ERR(page);
351 break;
352 }
353
354 limit = blkoff & ~(PAGE_SIZE - 1);
355
356 dbuf = page_address(page);
357
358 /* deal with the individual blocks stashed on this page */
359 do {
360 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
361 sizeof(union afs_dir_block)];
Al Viro1bbae9f2013-05-22 16:31:14 -0400362 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (ret != 1) {
364 afs_dir_put_page(page);
365 goto out;
366 }
367
368 blkoff += sizeof(union afs_dir_block);
369
Al Viro1bbae9f2013-05-22 16:31:14 -0400370 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 afs_dir_put_page(page);
373 ret = 0;
374 }
375
David Howellsec268152007-04-26 15:49:28 -0700376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 _leave(" = %d", ret);
378 return ret;
David Howellsec268152007-04-26 15:49:28 -0700379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * read an AFS directory
383 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400384static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Al Viro1bbae9f2013-05-22 16:31:14 -0400386 return afs_dir_iterate(file_inode(file),
387 ctx, file->private_data);
David Howellsec268152007-04-26 15:49:28 -0700388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
391 * search the directory for a name
392 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
393 * uniquifier through dtype
394 */
Miklos Szerediac7576f2014-10-30 17:37:34 +0100395static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
396 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100398 struct afs_lookup_cookie *cookie =
399 container_of(ctx, struct afs_lookup_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Al Viro1bbae9f2013-05-22 16:31:14 -0400401 _enter("{%s,%u},%s,%u,,%llu,%u",
402 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700403 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells08e0e7c2007-04-26 15:55:03 -0700405 /* insanity checks first */
406 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
407 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
408
Al Viro1bbae9f2013-05-22 16:31:14 -0400409 if (cookie->name.len != nlen ||
410 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 _leave(" = 0 [no]");
412 return 0;
413 }
414
415 cookie->fid.vnode = ino;
416 cookie->fid.unique = dtype;
417 cookie->found = 1;
418
419 _leave(" = -1 [found]");
420 return -1;
David Howellsec268152007-04-26 15:49:28 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
David Howells08e0e7c2007-04-26 15:55:03 -0700424 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700425 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
David Howells08e0e7c2007-04-26 15:55:03 -0700427static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700428 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Al Viro1bbae9f2013-05-22 16:31:14 -0400430 struct afs_super_info *as = dir->i_sb->s_fs_info;
431 struct afs_lookup_cookie cookie = {
432 .ctx.actor = afs_lookup_filldir,
433 .name = dentry->d_name,
434 .fid.vid = as->volume->vid
435 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int ret;
437
Al Viroa4555892014-10-21 20:11:25 -0400438 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400441 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700443 _leave(" = %d [iter]", ret);
444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 ret = -ENOENT;
448 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700449 _leave(" = -ENOENT [not found]");
450 return -ENOENT;
451 }
452
453 *fid = cookie.fid;
454 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
455 return 0;
456}
457
458/*
wangleibec5eb62010-08-11 09:38:04 +0100459 * Try to auto mount the mountpoint with pseudo directory, if the autocell
460 * operation is setted.
461 */
462static struct inode *afs_try_auto_mntpt(
463 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
464 struct afs_fid *fid)
465{
466 const char *devname = dentry->d_name.name;
467 struct afs_vnode *vnode = AFS_FS_I(dir);
468 struct inode *inode;
469
Al Viroa4555892014-10-21 20:11:25 -0400470 _enter("%d, %p{%pd}, {%x:%u}, %p",
471 ret, dentry, dentry, vnode->fid.vid, vnode->fid.vnode, key);
wangleibec5eb62010-08-11 09:38:04 +0100472
473 if (ret != -ENOENT ||
474 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
475 goto out;
476
477 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
478 if (IS_ERR(inode)) {
479 ret = PTR_ERR(inode);
480 goto out;
481 }
482
483 *fid = AFS_FS_I(inode)->fid;
484 _leave("= %p", inode);
485 return inode;
486
487out:
488 _leave("= %d", ret);
489 return ERR_PTR(ret);
490}
491
492/*
David Howells08e0e7c2007-04-26 15:55:03 -0700493 * look up an entry in a directory
494 */
David Howells260a9802007-04-26 15:59:35 -0700495static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400496 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700497{
498 struct afs_vnode *vnode;
499 struct afs_fid fid;
500 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700501 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700502 int ret;
503
David Howells260a9802007-04-26 15:59:35 -0700504 vnode = AFS_FS_I(dir);
505
Al Viroa4555892014-10-21 20:11:25 -0400506 _enter("{%x:%u},%p{%pd},",
507 vnode->fid.vid, vnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700508
David Howells2b0143b2015-03-17 22:25:59 +0000509 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700510
David Howells45222b92007-05-10 22:22:20 -0700511 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700512 _leave(" = -ENAMETOOLONG");
513 return ERR_PTR(-ENAMETOOLONG);
514 }
515
David Howells08e0e7c2007-04-26 15:55:03 -0700516 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
517 _leave(" = -ESTALE");
518 return ERR_PTR(-ESTALE);
519 }
520
David Howells00d3b7a2007-04-26 15:57:07 -0700521 key = afs_request_key(vnode->volume->cell);
522 if (IS_ERR(key)) {
523 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800524 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700525 }
526
David Howells260a9802007-04-26 15:59:35 -0700527 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700528 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700529 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700530 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return ERR_PTR(ret);
532 }
533
David Howells260a9802007-04-26 15:59:35 -0700534 ret = afs_do_lookup(dir, dentry, &fid, key);
535 if (ret < 0) {
wangleibec5eb62010-08-11 09:38:04 +0100536 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
537 if (!IS_ERR(inode)) {
538 key_put(key);
539 goto success;
540 }
541
542 ret = PTR_ERR(inode);
David Howells260a9802007-04-26 15:59:35 -0700543 key_put(key);
544 if (ret == -ENOENT) {
545 d_add(dentry, NULL);
546 _leave(" = NULL [negative]");
547 return NULL;
548 }
549 _leave(" = %d [do]", ret);
550 return ERR_PTR(ret);
551 }
552 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* instantiate the dentry */
David Howells260a9802007-04-26 15:59:35 -0700555 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700556 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700557 if (IS_ERR(inode)) {
558 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800559 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
wangleibec5eb62010-08-11 09:38:04 +0100562success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 d_add(dentry, inode);
David Howellsd6e43f72011-06-14 00:45:44 +0100564 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
David Howells08e0e7c2007-04-26 15:55:03 -0700565 fid.vnode,
566 fid.unique,
David Howells2b0143b2015-03-17 22:25:59 +0000567 d_inode(dentry)->i_ino,
568 d_inode(dentry)->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * check that a dentry lookup hit has found a valid entry
575 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
576 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Al Viro0b728e12012-06-10 16:03:43 -0400578static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
David Howells260a9802007-04-26 15:59:35 -0700580 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100581 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct dentry *parent;
David Howells00d3b7a2007-04-26 15:57:07 -0700583 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700584 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int ret;
586
Al Viro0b728e12012-06-10 16:03:43 -0400587 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100588 return -ECHILD;
589
David Howells2b0143b2015-03-17 22:25:59 +0000590 vnode = AFS_FS_I(d_inode(dentry));
David Howells08e0e7c2007-04-26 15:55:03 -0700591
David Howells2b0143b2015-03-17 22:25:59 +0000592 if (d_really_is_positive(dentry))
Al Viroa4555892014-10-21 20:11:25 -0400593 _enter("{v={%x:%u} n=%pd fl=%lx},",
594 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700595 vnode->flags);
596 else
Al Viroa4555892014-10-21 20:11:25 -0400597 _enter("{neg n=%pd}", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
David Howells260a9802007-04-26 15:59:35 -0700599 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700600 if (IS_ERR(key))
601 key = NULL;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700604 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000605 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David Howells260a9802007-04-26 15:59:35 -0700607 /* validate the parent directory */
608 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
609 afs_validate(dir, key);
610
611 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -0400612 _debug("%pd: parent dir deleted", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto out_bad;
614 }
615
David Howells260a9802007-04-26 15:59:35 -0700616 dir_version = (void *) (unsigned long) dir->status.data_version;
617 if (dentry->d_fsdata == dir_version)
618 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
David Howells260a9802007-04-26 15:59:35 -0700620 _debug("dir modified");
621
622 /* search the directory for this vnode */
623 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
624 switch (ret) {
625 case 0:
626 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +0000627 if (d_really_is_negative(dentry))
David Howells260a9802007-04-26 15:59:35 -0700628 goto out_bad;
David Howells2b0143b2015-03-17 22:25:59 +0000629 if (is_bad_inode(d_inode(dentry))) {
Al Viroa4555892014-10-21 20:11:25 -0400630 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
631 dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto out_bad;
633 }
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* if the vnode ID has changed, then the dirent points to a
636 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700637 if (fid.vnode != vnode->fid.vnode) {
Al Viroa4555892014-10-21 20:11:25 -0400638 _debug("%pd: dirent changed [%u != %u]",
639 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -0700640 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto not_found;
642 }
643
644 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700645 * been deleted and replaced, and the original vnode ID has
646 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700647 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -0400648 _debug("%pd: file deleted (uq %u -> %u I:%u)",
649 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500650 vnode->fid.unique,
David Howells2b0143b2015-03-17 22:25:59 +0000651 d_inode(dentry)->i_generation);
David Howells08e0e7c2007-04-26 15:55:03 -0700652 spin_lock(&vnode->lock);
653 set_bit(AFS_VNODE_DELETED, &vnode->flags);
654 spin_unlock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -0700655 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
David Howells260a9802007-04-26 15:59:35 -0700657 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
David Howells260a9802007-04-26 15:59:35 -0700659 case -ENOENT:
660 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -0400661 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000662 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -0700663 goto not_found;
664 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700665
David Howells260a9802007-04-26 15:59:35 -0700666 default:
Al Viroa4555892014-10-21 20:11:25 -0400667 _debug("failed to iterate dir %pd: %d",
668 parent, ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700669 goto out_bad;
670 }
671
David Howellsec268152007-04-26 15:49:28 -0700672out_valid:
David Howells260a9802007-04-26 15:59:35 -0700673 dentry->d_fsdata = dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700675 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 _leave(" = 1 [valid]");
677 return 1;
678
679 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700680not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_lock(&dentry->d_lock);
682 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
683 spin_unlock(&dentry->d_lock);
684
David Howellsec268152007-04-26 15:49:28 -0700685out_bad:
Al Viroa4555892014-10-21 20:11:25 -0400686 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700688 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 _leave(" = 0 [bad]");
691 return 0;
David Howellsec268152007-04-26 15:49:28 -0700692}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
695 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
696 * sleep)
697 * - called from dput() when d_count is going to 0.
698 * - return 1 to request dentry be unhashed, 0 otherwise
699 */
Nick Pigginfe15ce42011-01-07 17:49:23 +1100700static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Al Viroa4555892014-10-21 20:11:25 -0400702 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
705 goto zap;
706
David Howells2b0143b2015-03-17 22:25:59 +0000707 if (d_really_is_positive(dentry) &&
708 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
709 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +0100710 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 _leave(" = 0 [keep]");
713 return 0;
714
David Howellsec268152007-04-26 15:49:28 -0700715zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 _leave(" = 1 [zap]");
717 return 1;
David Howellsec268152007-04-26 15:49:28 -0700718}
David Howells260a9802007-04-26 15:59:35 -0700719
720/*
721 * handle dentry release
722 */
723static void afs_d_release(struct dentry *dentry)
724{
Al Viroa4555892014-10-21 20:11:25 -0400725 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -0700726}
727
728/*
729 * create a directory on an AFS filesystem
730 */
Al Viro18bb1db2011-07-26 01:41:39 -0400731static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -0700732{
733 struct afs_file_status status;
734 struct afs_callback cb;
735 struct afs_server *server;
736 struct afs_vnode *dvnode, *vnode;
737 struct afs_fid fid;
738 struct inode *inode;
739 struct key *key;
740 int ret;
741
742 dvnode = AFS_FS_I(dir);
743
Al Viroa4555892014-10-21 20:11:25 -0400744 _enter("{%x:%u},{%pd},%ho",
745 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -0700746
David Howells260a9802007-04-26 15:59:35 -0700747 key = afs_request_key(dvnode->volume->cell);
748 if (IS_ERR(key)) {
749 ret = PTR_ERR(key);
750 goto error;
751 }
752
753 mode |= S_IFDIR;
754 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
755 mode, &fid, &status, &cb, &server);
756 if (ret < 0)
757 goto mkdir_error;
758
759 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
760 if (IS_ERR(inode)) {
761 /* ENOMEM at a really inconvenient time - just abandon the new
762 * directory on the server */
763 ret = PTR_ERR(inode);
764 goto iget_error;
765 }
766
767 /* apply the status report we've got for the new vnode */
768 vnode = AFS_FS_I(inode);
769 spin_lock(&vnode->lock);
770 vnode->update_cnt++;
771 spin_unlock(&vnode->lock);
772 afs_vnode_finalise_status_update(vnode, server);
773 afs_put_server(server);
774
775 d_instantiate(dentry, inode);
776 if (d_unhashed(dentry)) {
777 _debug("not hashed");
778 d_rehash(dentry);
779 }
780 key_put(key);
781 _leave(" = 0");
782 return 0;
783
784iget_error:
785 afs_put_server(server);
786mkdir_error:
787 key_put(key);
788error:
789 d_drop(dentry);
790 _leave(" = %d", ret);
791 return ret;
792}
793
794/*
795 * remove a directory from an AFS filesystem
796 */
797static int afs_rmdir(struct inode *dir, struct dentry *dentry)
798{
799 struct afs_vnode *dvnode, *vnode;
800 struct key *key;
801 int ret;
802
803 dvnode = AFS_FS_I(dir);
804
Al Viroa4555892014-10-21 20:11:25 -0400805 _enter("{%x:%u},{%pd}",
806 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -0700807
David Howells260a9802007-04-26 15:59:35 -0700808 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
David Howells2b0143b2015-03-17 22:25:59 +0000818 if (d_really_is_positive(dentry)) {
819 vnode = AFS_FS_I(d_inode(dentry));
David Howells260a9802007-04-26 15:59:35 -0700820 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
Al Viroa4555892014-10-21 20:11:25 -0400847 _enter("{%x:%u},{%pd}",
848 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -0700849
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
David Howells2b0143b2015-03-17 22:25:59 +0000860 if (d_really_is_positive(dentry)) {
861 vnode = AFS_FS_I(d_inode(dentry));
David Howells260a9802007-04-26 15:59:35 -0700862
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
David Howells2b0143b2015-03-17 22:25:59 +0000873 if (d_really_is_positive(dentry)) {
David Howells260a9802007-04-26 15:59:35 -0700874 /* 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 */
David Howells2b0143b2015-03-17 22:25:59 +0000883 vnode = AFS_FS_I(d_inode(dentry));
David Howells260a9802007-04-26 15:59:35 -0700884 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 */
Al Viro4acdaf22011-07-26 01:42:34 -0400907static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400908 bool excl)
David Howells260a9802007-04-26 15:59:35 -0700909{
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
Al Viroa4555892014-10-21 20:11:25 -0400921 _enter("{%x:%u},{%pd},%ho,",
922 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -0700923
David Howells260a9802007-04-26 15:59:35 -0700924 key = afs_request_key(dvnode->volume->cell);
925 if (IS_ERR(key)) {
926 ret = PTR_ERR(key);
927 goto error;
928 }
929
930 mode |= S_IFREG;
931 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
932 mode, &fid, &status, &cb, &server);
933 if (ret < 0)
934 goto create_error;
935
936 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
937 if (IS_ERR(inode)) {
938 /* ENOMEM at a really inconvenient time - just abandon the new
939 * directory on the server */
940 ret = PTR_ERR(inode);
941 goto iget_error;
942 }
943
944 /* apply the status report we've got for the new vnode */
945 vnode = AFS_FS_I(inode);
946 spin_lock(&vnode->lock);
947 vnode->update_cnt++;
948 spin_unlock(&vnode->lock);
949 afs_vnode_finalise_status_update(vnode, server);
950 afs_put_server(server);
951
952 d_instantiate(dentry, inode);
953 if (d_unhashed(dentry)) {
954 _debug("not hashed");
955 d_rehash(dentry);
956 }
957 key_put(key);
958 _leave(" = 0");
959 return 0;
960
961iget_error:
962 afs_put_server(server);
963create_error:
964 key_put(key);
965error:
966 d_drop(dentry);
967 _leave(" = %d", ret);
968 return ret;
969}
970
971/*
972 * create a hard link between files in an AFS filesystem
973 */
974static int afs_link(struct dentry *from, struct inode *dir,
975 struct dentry *dentry)
976{
977 struct afs_vnode *dvnode, *vnode;
978 struct key *key;
979 int ret;
980
David Howells2b0143b2015-03-17 22:25:59 +0000981 vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -0700982 dvnode = AFS_FS_I(dir);
983
Al Viroa4555892014-10-21 20:11:25 -0400984 _enter("{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -0700985 vnode->fid.vid, vnode->fid.vnode,
986 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -0400987 dentry);
David Howells260a9802007-04-26 15:59:35 -0700988
David Howells260a9802007-04-26 15:59:35 -0700989 key = afs_request_key(dvnode->volume->cell);
990 if (IS_ERR(key)) {
991 ret = PTR_ERR(key);
992 goto error;
993 }
994
995 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
996 if (ret < 0)
997 goto link_error;
998
Al Viro7de9c6ee2010-10-23 11:11:40 -0400999 ihold(&vnode->vfs_inode);
David Howells260a9802007-04-26 15:59:35 -07001000 d_instantiate(dentry, &vnode->vfs_inode);
1001 key_put(key);
1002 _leave(" = 0");
1003 return 0;
1004
1005link_error:
1006 key_put(key);
1007error:
1008 d_drop(dentry);
1009 _leave(" = %d", ret);
1010 return ret;
1011}
1012
1013/*
1014 * create a symlink in an AFS filesystem
1015 */
1016static int afs_symlink(struct inode *dir, struct dentry *dentry,
1017 const char *content)
1018{
1019 struct afs_file_status status;
1020 struct afs_server *server;
1021 struct afs_vnode *dvnode, *vnode;
1022 struct afs_fid fid;
1023 struct inode *inode;
1024 struct key *key;
1025 int ret;
1026
1027 dvnode = AFS_FS_I(dir);
1028
Al Viroa4555892014-10-21 20:11:25 -04001029 _enter("{%x:%u},{%pd},%s",
1030 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001031 content);
1032
David Howells260a9802007-04-26 15:59:35 -07001033 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001034 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001035 goto error;
1036
1037 key = afs_request_key(dvnode->volume->cell);
1038 if (IS_ERR(key)) {
1039 ret = PTR_ERR(key);
1040 goto error;
1041 }
1042
1043 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1044 &fid, &status, &server);
1045 if (ret < 0)
1046 goto create_error;
1047
1048 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1049 if (IS_ERR(inode)) {
1050 /* ENOMEM at a really inconvenient time - just abandon the new
1051 * directory on the server */
1052 ret = PTR_ERR(inode);
1053 goto iget_error;
1054 }
1055
1056 /* apply the status report we've got for the new vnode */
1057 vnode = AFS_FS_I(inode);
1058 spin_lock(&vnode->lock);
1059 vnode->update_cnt++;
1060 spin_unlock(&vnode->lock);
1061 afs_vnode_finalise_status_update(vnode, server);
1062 afs_put_server(server);
1063
1064 d_instantiate(dentry, inode);
1065 if (d_unhashed(dentry)) {
1066 _debug("not hashed");
1067 d_rehash(dentry);
1068 }
1069 key_put(key);
1070 _leave(" = 0");
1071 return 0;
1072
1073iget_error:
1074 afs_put_server(server);
1075create_error:
1076 key_put(key);
1077error:
1078 d_drop(dentry);
1079 _leave(" = %d", ret);
1080 return ret;
1081}
1082
1083/*
1084 * rename a file in an AFS filesystem and/or move it between directories
1085 */
1086static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001087 struct inode *new_dir, struct dentry *new_dentry,
1088 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001089{
1090 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1091 struct key *key;
1092 int ret;
1093
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001094 if (flags)
1095 return -EINVAL;
1096
David Howells2b0143b2015-03-17 22:25:59 +00001097 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001098 orig_dvnode = AFS_FS_I(old_dir);
1099 new_dvnode = AFS_FS_I(new_dir);
1100
Al Viroa4555892014-10-21 20:11:25 -04001101 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001102 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1103 vnode->fid.vid, vnode->fid.vnode,
1104 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001105 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001106
David Howells260a9802007-04-26 15:59:35 -07001107 key = afs_request_key(orig_dvnode->volume->cell);
1108 if (IS_ERR(key)) {
1109 ret = PTR_ERR(key);
1110 goto error;
1111 }
1112
1113 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1114 old_dentry->d_name.name,
1115 new_dentry->d_name.name);
1116 if (ret < 0)
1117 goto rename_error;
1118 key_put(key);
1119 _leave(" = 0");
1120 return 0;
1121
1122rename_error:
1123 key_put(key);
1124error:
1125 d_drop(new_dentry);
1126 _leave(" = %d", ret);
1127 return ret;
1128}