1/* 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>
15#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
18#include <linux/ctype.h>
19#include <linux/sched.h>
20#include "internal.h"
21
22static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23				 struct nameidata *nd);
24static int afs_dir_open(struct inode *inode, struct file *file);
25static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
26static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
27static int afs_d_delete(struct dentry *dentry);
28static void afs_d_release(struct dentry *dentry);
29static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
30				  loff_t fpos, u64 ino, unsigned dtype);
31static 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);
42
43const struct file_operations afs_dir_file_operations = {
44	.open		= afs_dir_open,
45	.release	= afs_release,
46	.readdir	= afs_readdir,
47};
48
49const struct inode_operations afs_dir_inode_operations = {
50	.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,
58	.permission	= afs_permission,
59	.getattr	= afs_getattr,
60	.setattr	= afs_setattr,
61};
62
63static struct dentry_operations afs_fs_dentry_operations = {
64	.d_revalidate	= afs_d_revalidate,
65	.d_delete	= afs_d_delete,
66	.d_release	= afs_d_release,
67};
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
118struct afs_lookup_cookie {
119	struct afs_fid	fid;
120	const char	*name;
121	size_t		nlen;
122	int		found;
123};
124
125/*
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
135	/* determine how many magic numbers there should be in this page */
136	latter = dir->i_size - page_offset(page);
137	if (latter >= PAGE_SIZE)
138		qty = PAGE_SIZE;
139	else
140		qty = latter;
141	qty /= sizeof(union afs_dir_block);
142
143	/* check them */
144	dbuf = page_address(page);
145	for (tmp = 0; tmp < qty; tmp++) {
146		if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
147			printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
148			       __FUNCTION__, dir->i_ino, tmp, qty,
149			       ntohs(dbuf->blocks[tmp].pagehdr.magic));
150			goto error;
151		}
152	}
153
154	SetPageChecked(page);
155	return;
156
157error:
158	SetPageChecked(page);
159	SetPageError(page);
160}
161
162/*
163 * discard a page cached in the pagecache
164 */
165static inline void afs_dir_put_page(struct page *page)
166{
167	kunmap(page);
168	page_cache_release(page);
169}
170
171/*
172 * get a page into the pagecache
173 */
174static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
175				     struct key *key)
176{
177	struct page *page;
178	struct file file = {
179		.private_data = key,
180	};
181
182	_enter("{%lu},%lu", dir->i_ino, index);
183
184	page = read_mapping_page(dir->i_mapping, index, &file);
185	if (!IS_ERR(page)) {
186		kmap(page);
187		if (!PageChecked(page))
188			afs_dir_check_page(dir, page);
189		if (PageError(page))
190			goto fail;
191	}
192	return page;
193
194fail:
195	afs_dir_put_page(page);
196	_leave(" = -EIO");
197	return ERR_PTR(-EIO);
198}
199
200/*
201 * open an AFS directory file
202 */
203static int afs_dir_open(struct inode *inode, struct file *file)
204{
205	_enter("{%lu}", inode->i_ino);
206
207	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
208	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
209
210	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
211		return -ENOENT;
212
213	return afs_open(inode, file);
214}
215
216/*
217 * deal with one block in an AFS directory
218 */
219static int afs_dir_iterate_block(unsigned *fpos,
220				 union afs_dir_block *block,
221				 unsigned blkoff,
222				 void *cookie,
223				 filldir_t filldir)
224{
225	union afs_dirent *dire;
226	unsigned offset, next, curr;
227	size_t nlen;
228	int tmp, ret;
229
230	_enter("%u,%x,%p,,",*fpos,blkoff,block);
231
232	curr = (*fpos - blkoff) / sizeof(union afs_dirent);
233
234	/* walk through the block, an entry at a time */
235	for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
236	     offset < AFS_DIRENT_PER_BLOCK;
237	     offset = next
238	     ) {
239		next = offset + 1;
240
241		/* skip entries marked unused in the bitmap */
242		if (!(block->pagehdr.bitmap[offset / 8] &
243		      (1 << (offset % 8)))) {
244			_debug("ENT[%Zu.%u]: unused",
245			       blkoff / sizeof(union afs_dir_block), offset);
246			if (offset >= curr)
247				*fpos = blkoff +
248					next * sizeof(union afs_dirent);
249			continue;
250		}
251
252		/* got a valid entry */
253		dire = &block->dirents[offset];
254		nlen = strnlen(dire->u.name,
255			       sizeof(*block) -
256			       offset * sizeof(union afs_dirent));
257
258		_debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
259		       blkoff / sizeof(union afs_dir_block), offset,
260		       (offset < curr ? "skip" : "fill"),
261		       nlen, dire->u.name);
262
263		/* work out where the next possible entry is */
264		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
265			if (next >= AFS_DIRENT_PER_BLOCK) {
266				_debug("ENT[%Zu.%u]:"
267				       " %u travelled beyond end dir block"
268				       " (len %u/%Zu)",
269				       blkoff / sizeof(union afs_dir_block),
270				       offset, next, tmp, nlen);
271				return -EIO;
272			}
273			if (!(block->pagehdr.bitmap[next / 8] &
274			      (1 << (next % 8)))) {
275				_debug("ENT[%Zu.%u]:"
276				       " %u unmarked extension (len %u/%Zu)",
277				       blkoff / sizeof(union afs_dir_block),
278				       offset, next, tmp, nlen);
279				return -EIO;
280			}
281
282			_debug("ENT[%Zu.%u]: ext %u/%Zu",
283			       blkoff / sizeof(union afs_dir_block),
284			       next, tmp, nlen);
285			next++;
286		}
287
288		/* skip if starts before the current position */
289		if (offset < curr)
290			continue;
291
292		/* found the next entry */
293		ret = filldir(cookie,
294			      dire->u.name,
295			      nlen,
296			      blkoff + offset * sizeof(union afs_dirent),
297			      ntohl(dire->u.vnode),
298			      filldir == afs_lookup_filldir ?
299			      ntohl(dire->u.unique) : DT_UNKNOWN);
300		if (ret < 0) {
301			_leave(" = 0 [full]");
302			return 0;
303		}
304
305		*fpos = blkoff + next * sizeof(union afs_dirent);
306	}
307
308	_leave(" = 1 [more]");
309	return 1;
310}
311
312/*
313 * iterate through the data blob that lists the contents of an AFS directory
314 */
315static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
316			   filldir_t filldir, struct key *key)
317{
318	union afs_dir_block *dblock;
319	struct afs_dir_page *dbuf;
320	struct page *page;
321	unsigned blkoff, limit;
322	int ret;
323
324	_enter("{%lu},%u,,", dir->i_ino, *fpos);
325
326	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
327		_leave(" = -ESTALE");
328		return -ESTALE;
329	}
330
331	/* round the file position up to the next entry boundary */
332	*fpos += sizeof(union afs_dirent) - 1;
333	*fpos &= ~(sizeof(union afs_dirent) - 1);
334
335	/* walk through the blocks in sequence */
336	ret = 0;
337	while (*fpos < dir->i_size) {
338		blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
339
340		/* fetch the appropriate page from the directory */
341		page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
342		if (IS_ERR(page)) {
343			ret = PTR_ERR(page);
344			break;
345		}
346
347		limit = blkoff & ~(PAGE_SIZE - 1);
348
349		dbuf = page_address(page);
350
351		/* deal with the individual blocks stashed on this page */
352		do {
353			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
354					       sizeof(union afs_dir_block)];
355			ret = afs_dir_iterate_block(fpos, dblock, blkoff,
356						    cookie, filldir);
357			if (ret != 1) {
358				afs_dir_put_page(page);
359				goto out;
360			}
361
362			blkoff += sizeof(union afs_dir_block);
363
364		} while (*fpos < dir->i_size && blkoff < limit);
365
366		afs_dir_put_page(page);
367		ret = 0;
368	}
369
370out:
371	_leave(" = %d", ret);
372	return ret;
373}
374
375/*
376 * read an AFS directory
377 */
378static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
379{
380	unsigned fpos;
381	int ret;
382
383	_enter("{%Ld,{%lu}}",
384	       file->f_pos, file->f_path.dentry->d_inode->i_ino);
385
386	ASSERT(file->private_data != NULL);
387
388	fpos = file->f_pos;
389	ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
390			      cookie, filldir, file->private_data);
391	file->f_pos = fpos;
392
393	_leave(" = %d", ret);
394	return ret;
395}
396
397/*
398 * search the directory for a name
399 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
400 *   uniquifier through dtype
401 */
402static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
403			      loff_t fpos, u64 ino, unsigned dtype)
404{
405	struct afs_lookup_cookie *cookie = _cookie;
406
407	_enter("{%s,%Zu},%s,%u,,%llu,%u",
408	       cookie->name, cookie->nlen, name, nlen,
409	       (unsigned long long) ino, dtype);
410
411	/* insanity checks first */
412	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
413	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
414
415	if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
416		_leave(" = 0 [no]");
417		return 0;
418	}
419
420	cookie->fid.vnode = ino;
421	cookie->fid.unique = dtype;
422	cookie->found = 1;
423
424	_leave(" = -1 [found]");
425	return -1;
426}
427
428/*
429 * do a lookup in a directory
430 * - just returns the FID the dentry name maps to if found
431 */
432static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
433			 struct afs_fid *fid, struct key *key)
434{
435	struct afs_lookup_cookie cookie;
436	struct afs_super_info *as;
437	unsigned fpos;
438	int ret;
439
440	_enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
441
442	as = dir->i_sb->s_fs_info;
443
444	/* search the directory */
445	cookie.name	= dentry->d_name.name;
446	cookie.nlen	= dentry->d_name.len;
447	cookie.fid.vid	= as->volume->vid;
448	cookie.found	= 0;
449
450	fpos = 0;
451	ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
452			      key);
453	if (ret < 0) {
454		_leave(" = %d [iter]", ret);
455		return ret;
456	}
457
458	ret = -ENOENT;
459	if (!cookie.found) {
460		_leave(" = -ENOENT [not found]");
461		return -ENOENT;
462	}
463
464	*fid = cookie.fid;
465	_leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
466	return 0;
467}
468
469/*
470 * look up an entry in a directory
471 */
472static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
473				 struct nameidata *nd)
474{
475	struct afs_vnode *vnode;
476	struct afs_fid fid;
477	struct inode *inode;
478	struct key *key;
479	int ret;
480
481	vnode = AFS_FS_I(dir);
482
483	_enter("{%x:%u},%p{%s},",
484	       vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
485
486	ASSERTCMP(dentry->d_inode, ==, NULL);
487
488	if (dentry->d_name.len >= AFSNAMEMAX) {
489		_leave(" = -ENAMETOOLONG");
490		return ERR_PTR(-ENAMETOOLONG);
491	}
492
493	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
494		_leave(" = -ESTALE");
495		return ERR_PTR(-ESTALE);
496	}
497
498	key = afs_request_key(vnode->volume->cell);
499	if (IS_ERR(key)) {
500		_leave(" = %ld [key]", PTR_ERR(key));
501		return ERR_PTR(PTR_ERR(key));
502	}
503
504	ret = afs_validate(vnode, key);
505	if (ret < 0) {
506		key_put(key);
507		_leave(" = %d [val]", ret);
508		return ERR_PTR(ret);
509	}
510
511	ret = afs_do_lookup(dir, dentry, &fid, key);
512	if (ret < 0) {
513		key_put(key);
514		if (ret == -ENOENT) {
515			d_add(dentry, NULL);
516			_leave(" = NULL [negative]");
517			return NULL;
518		}
519		_leave(" = %d [do]", ret);
520		return ERR_PTR(ret);
521	}
522	dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
523
524	/* instantiate the dentry */
525	inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
526	key_put(key);
527	if (IS_ERR(inode)) {
528		_leave(" = %ld", PTR_ERR(inode));
529		return ERR_PTR(PTR_ERR(inode));
530	}
531
532	dentry->d_op = &afs_fs_dentry_operations;
533
534	d_add(dentry, inode);
535	_leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
536	       fid.vnode,
537	       fid.unique,
538	       dentry->d_inode->i_ino,
539	       dentry->d_inode->i_version);
540
541	return NULL;
542}
543
544/*
545 * check that a dentry lookup hit has found a valid entry
546 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
547 *   inode
548 */
549static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
550{
551	struct afs_vnode *vnode, *dir;
552	struct afs_fid fid;
553	struct dentry *parent;
554	struct key *key;
555	void *dir_version;
556	int ret;
557
558	vnode = AFS_FS_I(dentry->d_inode);
559
560	if (dentry->d_inode)
561		_enter("{v={%x:%u} n=%s fl=%lx},",
562		       vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
563		       vnode->flags);
564	else
565		_enter("{neg n=%s}", dentry->d_name.name);
566
567	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
568	if (IS_ERR(key))
569		key = NULL;
570
571	/* lock down the parent dentry so we can peer at it */
572	parent = dget_parent(dentry);
573	if (!parent->d_inode)
574		goto out_bad;
575
576	dir = AFS_FS_I(parent->d_inode);
577
578	/* validate the parent directory */
579	if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
580		afs_validate(dir, key);
581
582	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
583		_debug("%s: parent dir deleted", dentry->d_name.name);
584		goto out_bad;
585	}
586
587	dir_version = (void *) (unsigned long) dir->status.data_version;
588	if (dentry->d_fsdata == dir_version)
589		goto out_valid; /* the dir contents are unchanged */
590
591	_debug("dir modified");
592
593	/* search the directory for this vnode */
594	ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
595	switch (ret) {
596	case 0:
597		/* the filename maps to something */
598		if (!dentry->d_inode)
599			goto out_bad;
600		if (is_bad_inode(dentry->d_inode)) {
601			printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
602			       parent->d_name.name, dentry->d_name.name);
603			goto out_bad;
604		}
605
606		/* if the vnode ID has changed, then the dirent points to a
607		 * different file */
608		if (fid.vnode != vnode->fid.vnode) {
609			_debug("%s: dirent changed [%u != %u]",
610			       dentry->d_name.name, fid.vnode,
611			       vnode->fid.vnode);
612			goto not_found;
613		}
614
615		/* if the vnode ID uniqifier has changed, then the file has
616		 * been deleted and replaced, and the original vnode ID has
617		 * been reused */
618		if (fid.unique != vnode->fid.unique) {
619			_debug("%s: file deleted (uq %u -> %u I:%lu)",
620			       dentry->d_name.name, fid.unique,
621			       vnode->fid.unique, dentry->d_inode->i_version);
622			spin_lock(&vnode->lock);
623			set_bit(AFS_VNODE_DELETED, &vnode->flags);
624			spin_unlock(&vnode->lock);
625			goto not_found;
626		}
627		goto out_valid;
628
629	case -ENOENT:
630		/* the filename is unknown */
631		_debug("%s: dirent not found", dentry->d_name.name);
632		if (dentry->d_inode)
633			goto not_found;
634		goto out_valid;
635
636	default:
637		_debug("failed to iterate dir %s: %d",
638		       parent->d_name.name, ret);
639		goto out_bad;
640	}
641
642out_valid:
643	dentry->d_fsdata = dir_version;
644out_skip:
645	dput(parent);
646	key_put(key);
647	_leave(" = 1 [valid]");
648	return 1;
649
650	/* the dirent, if it exists, now points to a different vnode */
651not_found:
652	spin_lock(&dentry->d_lock);
653	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
654	spin_unlock(&dentry->d_lock);
655
656out_bad:
657	if (dentry->d_inode) {
658		/* don't unhash if we have submounts */
659		if (have_submounts(dentry))
660			goto out_skip;
661	}
662
663	_debug("dropping dentry %s/%s",
664	       parent->d_name.name, dentry->d_name.name);
665	shrink_dcache_parent(dentry);
666	d_drop(dentry);
667	dput(parent);
668	key_put(key);
669
670	_leave(" = 0 [bad]");
671	return 0;
672}
673
674/*
675 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
676 * sleep)
677 * - called from dput() when d_count is going to 0.
678 * - return 1 to request dentry be unhashed, 0 otherwise
679 */
680static int afs_d_delete(struct dentry *dentry)
681{
682	_enter("%s", dentry->d_name.name);
683
684	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
685		goto zap;
686
687	if (dentry->d_inode &&
688	    test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
689			goto zap;
690
691	_leave(" = 0 [keep]");
692	return 0;
693
694zap:
695	_leave(" = 1 [zap]");
696	return 1;
697}
698
699/*
700 * handle dentry release
701 */
702static void afs_d_release(struct dentry *dentry)
703{
704	_enter("%s", dentry->d_name.name);
705}
706
707/*
708 * create a directory on an AFS filesystem
709 */
710static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
711{
712	struct afs_file_status status;
713	struct afs_callback cb;
714	struct afs_server *server;
715	struct afs_vnode *dvnode, *vnode;
716	struct afs_fid fid;
717	struct inode *inode;
718	struct key *key;
719	int ret;
720
721	dvnode = AFS_FS_I(dir);
722
723	_enter("{%x:%u},{%s},%o",
724	       dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
725
726	ret = -ENAMETOOLONG;
727	if (dentry->d_name.len >= AFSNAMEMAX)
728		goto error;
729
730	key = afs_request_key(dvnode->volume->cell);
731	if (IS_ERR(key)) {
732		ret = PTR_ERR(key);
733		goto error;
734	}
735
736	mode |= S_IFDIR;
737	ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
738			       mode, &fid, &status, &cb, &server);
739	if (ret < 0)
740		goto mkdir_error;
741
742	inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
743	if (IS_ERR(inode)) {
744		/* ENOMEM at a really inconvenient time - just abandon the new
745		 * directory on the server */
746		ret = PTR_ERR(inode);
747		goto iget_error;
748	}
749
750	/* apply the status report we've got for the new vnode */
751	vnode = AFS_FS_I(inode);
752	spin_lock(&vnode->lock);
753	vnode->update_cnt++;
754	spin_unlock(&vnode->lock);
755	afs_vnode_finalise_status_update(vnode, server);
756	afs_put_server(server);
757
758	d_instantiate(dentry, inode);
759	if (d_unhashed(dentry)) {
760		_debug("not hashed");
761		d_rehash(dentry);
762	}
763	key_put(key);
764	_leave(" = 0");
765	return 0;
766
767iget_error:
768	afs_put_server(server);
769mkdir_error:
770	key_put(key);
771error:
772	d_drop(dentry);
773	_leave(" = %d", ret);
774	return ret;
775}
776
777/*
778 * remove a directory from an AFS filesystem
779 */
780static int afs_rmdir(struct inode *dir, struct dentry *dentry)
781{
782	struct afs_vnode *dvnode, *vnode;
783	struct key *key;
784	int ret;
785
786	dvnode = AFS_FS_I(dir);
787
788	_enter("{%x:%u},{%s}",
789	       dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
790
791	ret = -ENAMETOOLONG;
792	if (dentry->d_name.len >= AFSNAMEMAX)
793		goto error;
794
795	key = afs_request_key(dvnode->volume->cell);
796	if (IS_ERR(key)) {
797		ret = PTR_ERR(key);
798		goto error;
799	}
800
801	ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
802	if (ret < 0)
803		goto rmdir_error;
804
805	if (dentry->d_inode) {
806		vnode = AFS_FS_I(dentry->d_inode);
807		clear_nlink(&vnode->vfs_inode);
808		set_bit(AFS_VNODE_DELETED, &vnode->flags);
809		afs_discard_callback_on_delete(vnode);
810	}
811
812	key_put(key);
813	_leave(" = 0");
814	return 0;
815
816rmdir_error:
817	key_put(key);
818error:
819	_leave(" = %d", ret);
820	return ret;
821}
822
823/*
824 * remove a file from an AFS filesystem
825 */
826static int afs_unlink(struct inode *dir, struct dentry *dentry)
827{
828	struct afs_vnode *dvnode, *vnode;
829	struct key *key;
830	int ret;
831
832	dvnode = AFS_FS_I(dir);
833
834	_enter("{%x:%u},{%s}",
835	       dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
836
837	ret = -ENAMETOOLONG;
838	if (dentry->d_name.len >= AFSNAMEMAX)
839		goto error;
840
841	key = afs_request_key(dvnode->volume->cell);
842	if (IS_ERR(key)) {
843		ret = PTR_ERR(key);
844		goto error;
845	}
846
847	if (dentry->d_inode) {
848		vnode = AFS_FS_I(dentry->d_inode);
849
850		/* make sure we have a callback promise on the victim */
851		ret = afs_validate(vnode, key);
852		if (ret < 0)
853			goto error;
854	}
855
856	ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
857	if (ret < 0)
858		goto remove_error;
859
860	if (dentry->d_inode) {
861		/* if the file wasn't deleted due to excess hard links, the
862		 * fileserver will break the callback promise on the file - if
863		 * it had one - before it returns to us, and if it was deleted,
864		 * it won't
865		 *
866		 * however, if we didn't have a callback promise outstanding,
867		 * or it was outstanding on a different server, then it won't
868		 * break it either...
869		 */
870		vnode = AFS_FS_I(dentry->d_inode);
871		if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
872			_debug("AFS_VNODE_DELETED");
873		if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
874			_debug("AFS_VNODE_CB_BROKEN");
875		set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
876		ret = afs_validate(vnode, key);
877		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
878	}
879
880	key_put(key);
881	_leave(" = 0");
882	return 0;
883
884remove_error:
885	key_put(key);
886error:
887	_leave(" = %d", ret);
888	return ret;
889}
890
891/*
892 * create a regular file on an AFS filesystem
893 */
894static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
895		      struct nameidata *nd)
896{
897	struct afs_file_status status;
898	struct afs_callback cb;
899	struct afs_server *server;
900	struct afs_vnode *dvnode, *vnode;
901	struct afs_fid fid;
902	struct inode *inode;
903	struct key *key;
904	int ret;
905
906	dvnode = AFS_FS_I(dir);
907
908	_enter("{%x:%u},{%s},%o,",
909	       dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
910
911	ret = -ENAMETOOLONG;
912	if (dentry->d_name.len >= AFSNAMEMAX)
913		goto error;
914
915	key = afs_request_key(dvnode->volume->cell);
916	if (IS_ERR(key)) {
917		ret = PTR_ERR(key);
918		goto error;
919	}
920
921	mode |= S_IFREG;
922	ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
923			       mode, &fid, &status, &cb, &server);
924	if (ret < 0)
925		goto create_error;
926
927	inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
928	if (IS_ERR(inode)) {
929		/* ENOMEM at a really inconvenient time - just abandon the new
930		 * directory on the server */
931		ret = PTR_ERR(inode);
932		goto iget_error;
933	}
934
935	/* apply the status report we've got for the new vnode */
936	vnode = AFS_FS_I(inode);
937	spin_lock(&vnode->lock);
938	vnode->update_cnt++;
939	spin_unlock(&vnode->lock);
940	afs_vnode_finalise_status_update(vnode, server);
941	afs_put_server(server);
942
943	d_instantiate(dentry, inode);
944	if (d_unhashed(dentry)) {
945		_debug("not hashed");
946		d_rehash(dentry);
947	}
948	key_put(key);
949	_leave(" = 0");
950	return 0;
951
952iget_error:
953	afs_put_server(server);
954create_error:
955	key_put(key);
956error:
957	d_drop(dentry);
958	_leave(" = %d", ret);
959	return ret;
960}
961
962/*
963 * create a hard link between files in an AFS filesystem
964 */
965static int afs_link(struct dentry *from, struct inode *dir,
966		    struct dentry *dentry)
967{
968	struct afs_vnode *dvnode, *vnode;
969	struct key *key;
970	int ret;
971
972	vnode = AFS_FS_I(from->d_inode);
973	dvnode = AFS_FS_I(dir);
974
975	_enter("{%x:%u},{%x:%u},{%s}",
976	       vnode->fid.vid, vnode->fid.vnode,
977	       dvnode->fid.vid, dvnode->fid.vnode,
978	       dentry->d_name.name);
979
980	ret = -ENAMETOOLONG;
981	if (dentry->d_name.len >= AFSNAMEMAX)
982		goto error;
983
984	key = afs_request_key(dvnode->volume->cell);
985	if (IS_ERR(key)) {
986		ret = PTR_ERR(key);
987		goto error;
988	}
989
990	ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
991	if (ret < 0)
992		goto link_error;
993
994	atomic_inc(&vnode->vfs_inode.i_count);
995	d_instantiate(dentry, &vnode->vfs_inode);
996	key_put(key);
997	_leave(" = 0");
998	return 0;
999
1000link_error:
1001	key_put(key);
1002error:
1003	d_drop(dentry);
1004	_leave(" = %d", ret);
1005	return ret;
1006}
1007
1008/*
1009 * create a symlink in an AFS filesystem
1010 */
1011static int afs_symlink(struct inode *dir, struct dentry *dentry,
1012		       const char *content)
1013{
1014	struct afs_file_status status;
1015	struct afs_server *server;
1016	struct afs_vnode *dvnode, *vnode;
1017	struct afs_fid fid;
1018	struct inode *inode;
1019	struct key *key;
1020	int ret;
1021
1022	dvnode = AFS_FS_I(dir);
1023
1024	_enter("{%x:%u},{%s},%s",
1025	       dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1026	       content);
1027
1028	ret = -ENAMETOOLONG;
1029	if (dentry->d_name.len >= AFSNAMEMAX)
1030		goto error;
1031
1032	ret = -EINVAL;
1033	if (strlen(content) >= AFSPATHMAX)
1034		goto error;
1035
1036	key = afs_request_key(dvnode->volume->cell);
1037	if (IS_ERR(key)) {
1038		ret = PTR_ERR(key);
1039		goto error;
1040	}
1041
1042	ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1043				&fid, &status, &server);
1044	if (ret < 0)
1045		goto create_error;
1046
1047	inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1048	if (IS_ERR(inode)) {
1049		/* ENOMEM at a really inconvenient time - just abandon the new
1050		 * directory on the server */
1051		ret = PTR_ERR(inode);
1052		goto iget_error;
1053	}
1054
1055	/* apply the status report we've got for the new vnode */
1056	vnode = AFS_FS_I(inode);
1057	spin_lock(&vnode->lock);
1058	vnode->update_cnt++;
1059	spin_unlock(&vnode->lock);
1060	afs_vnode_finalise_status_update(vnode, server);
1061	afs_put_server(server);
1062
1063	d_instantiate(dentry, inode);
1064	if (d_unhashed(dentry)) {
1065		_debug("not hashed");
1066		d_rehash(dentry);
1067	}
1068	key_put(key);
1069	_leave(" = 0");
1070	return 0;
1071
1072iget_error:
1073	afs_put_server(server);
1074create_error:
1075	key_put(key);
1076error:
1077	d_drop(dentry);
1078	_leave(" = %d", ret);
1079	return ret;
1080}
1081
1082/*
1083 * rename a file in an AFS filesystem and/or move it between directories
1084 */
1085static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1086		      struct inode *new_dir, struct dentry *new_dentry)
1087{
1088	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1089	struct key *key;
1090	int ret;
1091
1092	vnode = AFS_FS_I(old_dentry->d_inode);
1093	orig_dvnode = AFS_FS_I(old_dir);
1094	new_dvnode = AFS_FS_I(new_dir);
1095
1096	_enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
1097	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1098	       vnode->fid.vid, vnode->fid.vnode,
1099	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1100	       new_dentry->d_name.name);
1101
1102	ret = -ENAMETOOLONG;
1103	if (new_dentry->d_name.len >= AFSNAMEMAX)
1104		goto error;
1105
1106	key = afs_request_key(orig_dvnode->volume->cell);
1107	if (IS_ERR(key)) {
1108		ret = PTR_ERR(key);
1109		goto error;
1110	}
1111
1112	ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1113			       old_dentry->d_name.name,
1114			       new_dentry->d_name.name);
1115	if (ret < 0)
1116		goto rename_error;
1117	key_put(key);
1118	_leave(" = 0");
1119	return 0;
1120
1121rename_error:
1122	key_put(key);
1123error:
1124	d_drop(new_dentry);
1125	_leave(" = %d", ret);
1126	return ret;
1127}
1128