xref: /openbmc/linux/fs/afs/dir_silly.c (revision 74a22e8f)
1 /* AFS silly rename handling
2  *
3  * Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from NFS's sillyrename.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public Licence
9  * as published by the Free Software Foundation; either version
10  * 2 of the Licence, or (at your option) any later version.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/fsnotify.h>
17 #include "internal.h"
18 
19 /*
20  * Actually perform the silly rename step.
21  */
22 static int afs_do_silly_rename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
23 			       struct dentry *old, struct dentry *new,
24 			       struct key *key)
25 {
26 	struct afs_fs_cursor fc;
27 	struct afs_status_cb *scb;
28 	int ret = -ERESTARTSYS;
29 
30 	_enter("%pd,%pd", old, new);
31 
32 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
33 	if (!scb)
34 		return -ENOMEM;
35 
36 	trace_afs_silly_rename(vnode, false);
37 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
38 		afs_dataversion_t dir_data_version = dvnode->status.data_version + 1;
39 
40 		while (afs_select_fileserver(&fc)) {
41 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
42 			afs_fs_rename(&fc, old->d_name.name,
43 				      dvnode, new->d_name.name,
44 				      scb, scb);
45 		}
46 
47 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
48 					&dir_data_version, scb);
49 		ret = afs_end_vnode_operation(&fc);
50 	}
51 
52 	if (ret == 0) {
53 		spin_lock(&old->d_lock);
54 		old->d_flags |= DCACHE_NFSFS_RENAMED;
55 		spin_unlock(&old->d_lock);
56 		if (dvnode->silly_key != key) {
57 			key_put(dvnode->silly_key);
58 			dvnode->silly_key = key_get(key);
59 		}
60 
61 		if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
62 			afs_edit_dir_remove(dvnode, &old->d_name,
63 					    afs_edit_dir_for_silly_0);
64 		if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
65 			afs_edit_dir_add(dvnode, &new->d_name,
66 					 &vnode->fid, afs_edit_dir_for_silly_1);
67 
68 		/* vfs_unlink and the like do not issue this when a file is
69 		 * sillyrenamed, so do it here.
70 		 */
71 		fsnotify_nameremove(old, 0);
72 	}
73 
74 	kfree(scb);
75 	_leave(" = %d", ret);
76 	return ret;
77 }
78 
79 /**
80  * afs_sillyrename - Perform a silly-rename of a dentry
81  *
82  * AFS is stateless and the server doesn't know when the client is holding a
83  * file open.  To prevent application problems when a file is unlinked while
84  * it's still open, the client performs a "silly-rename".  That is, it renames
85  * the file to a hidden file in the same directory, and only performs the
86  * unlink once the last reference to it is put.
87  *
88  * The final cleanup is done during dentry_iput.
89  */
90 int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
91 		    struct dentry *dentry, struct key *key)
92 {
93 	static unsigned int sillycounter;
94 	struct dentry *sdentry = NULL;
95 	unsigned char silly[16];
96 	int ret = -EBUSY;
97 
98 	_enter("");
99 
100 	/* We don't allow a dentry to be silly-renamed twice. */
101 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
102 		return -EBUSY;
103 
104 	sdentry = NULL;
105 	do {
106 		int slen;
107 
108 		dput(sdentry);
109 		sillycounter++;
110 
111 		/* Create a silly name.  Note that the ".__afs" prefix is
112 		 * understood by the salvager and must not be changed.
113 		 */
114 		slen = scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
115 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
116 
117 		/* N.B. Better to return EBUSY here ... it could be dangerous
118 		 * to delete the file while it's in use.
119 		 */
120 		if (IS_ERR(sdentry))
121 			goto out;
122 	} while (!d_is_negative(sdentry));
123 
124 	ihold(&vnode->vfs_inode);
125 
126 	ret = afs_do_silly_rename(dvnode, vnode, dentry, sdentry, key);
127 	switch (ret) {
128 	case 0:
129 		/* The rename succeeded. */
130 		d_move(dentry, sdentry);
131 		break;
132 	case -ERESTARTSYS:
133 		/* The result of the rename is unknown. Play it safe by forcing
134 		 * a new lookup.
135 		 */
136 		d_drop(dentry);
137 		d_drop(sdentry);
138 	}
139 
140 	iput(&vnode->vfs_inode);
141 	dput(sdentry);
142 out:
143 	_leave(" = %d", ret);
144 	return ret;
145 }
146 
147 /*
148  * Tell the server to remove a sillyrename file.
149  */
150 static int afs_do_silly_unlink(struct afs_vnode *dvnode, struct afs_vnode *vnode,
151 			       struct dentry *dentry, struct key *key)
152 {
153 	struct afs_fs_cursor fc;
154 	struct afs_status_cb *scb;
155 	int ret = -ERESTARTSYS;
156 
157 	_enter("");
158 
159 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
160 	if (!scb)
161 		return -ENOMEM;
162 
163 	trace_afs_silly_rename(vnode, true);
164 	if (afs_begin_vnode_operation(&fc, dvnode, key, false)) {
165 		afs_dataversion_t dir_data_version = dvnode->status.data_version + 1;
166 
167 		while (afs_select_fileserver(&fc)) {
168 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
169 
170 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
171 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
172 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
173 						    &scb[0], &scb[1]);
174 				if (fc.ac.error != -ECONNABORTED ||
175 				    fc.ac.abort_code != RXGEN_OPCODE)
176 					continue;
177 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
178 			}
179 
180 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
181 		}
182 
183 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
184 					&dir_data_version, &scb[0]);
185 		ret = afs_end_vnode_operation(&fc);
186 		if (ret == 0) {
187 			drop_nlink(&vnode->vfs_inode);
188 			if (vnode->vfs_inode.i_nlink == 0) {
189 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
190 				clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
191 			}
192 		}
193 		if (ret == 0 &&
194 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
195 			afs_edit_dir_remove(dvnode, &dentry->d_name,
196 					    afs_edit_dir_for_unlink);
197 	}
198 
199 	kfree(scb);
200 	_leave(" = %d", ret);
201 	return ret;
202 }
203 
204 /*
205  * Remove sillyrename file on iput.
206  */
207 int afs_silly_iput(struct dentry *dentry, struct inode *inode)
208 {
209 	struct afs_vnode *dvnode = AFS_FS_I(d_inode(dentry->d_parent));
210 	struct afs_vnode *vnode = AFS_FS_I(inode);
211 	struct dentry *alias;
212 	int ret;
213 
214 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
215 
216 	_enter("%p{%pd},%llx", dentry, dentry, vnode->fid.vnode);
217 
218 	down_read(&dvnode->rmdir_lock);
219 
220 	alias = d_alloc_parallel(dentry->d_parent, &dentry->d_name, &wq);
221 	if (IS_ERR(alias)) {
222 		up_read(&dvnode->rmdir_lock);
223 		return 0;
224 	}
225 
226 	if (!d_in_lookup(alias)) {
227 		/* We raced with lookup...  See if we need to transfer the
228 		 * sillyrename information to the aliased dentry.
229 		 */
230 		ret = 0;
231 		spin_lock(&alias->d_lock);
232 		if (d_really_is_positive(alias) &&
233 		    !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
234 			alias->d_flags |= DCACHE_NFSFS_RENAMED;
235 			ret = 1;
236 		}
237 		spin_unlock(&alias->d_lock);
238 		up_read(&dvnode->rmdir_lock);
239 		dput(alias);
240 		return ret;
241 	}
242 
243 	/* Stop lock-release from complaining. */
244 	spin_lock(&vnode->lock);
245 	vnode->lock_state = AFS_VNODE_LOCK_DELETED;
246 	trace_afs_flock_ev(vnode, NULL, afs_flock_silly_delete, 0);
247 	spin_unlock(&vnode->lock);
248 
249 	afs_do_silly_unlink(dvnode, vnode, dentry, dvnode->silly_key);
250 	up_read(&dvnode->rmdir_lock);
251 	d_lookup_done(alias);
252 	dput(alias);
253 	return 1;
254 }
255