blob: 9c7333eb01c2b8b804fcd28b821cf096d9cb7d0a [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS vnode management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
David Howellsc435ee32017-11-02 15:27:49 +000020 * Handle remote file deletion.
David Howells08e0e7c2007-04-26 15:55:03 -070021 */
22static void afs_vnode_deleted_remotely(struct afs_vnode *vnode)
23{
David Howellsc435ee32017-11-02 15:27:49 +000024 struct afs_cb_interest *cbi = vnode->cb_interest;
David Howells08e0e7c2007-04-26 15:55:03 -070025
David Howellsc435ee32017-11-02 15:27:49 +000026 _enter("{%p}", cbi);
David Howells0f300ca2007-05-10 22:22:20 -070027
David Howells08e0e7c2007-04-26 15:55:03 -070028 set_bit(AFS_VNODE_DELETED, &vnode->flags);
29
David Howellsc435ee32017-11-02 15:27:49 +000030 if (cbi) {
31 vnode->cb_interest = NULL;
32 afs_put_cb_interest(afs_v2net(vnode), cbi);
David Howells08e0e7c2007-04-26 15:55:03 -070033 }
34
David Howells0f300ca2007-05-10 22:22:20 -070035 _leave("");
David Howells08e0e7c2007-04-26 15:55:03 -070036}
37
38/*
David Howells260a9802007-04-26 15:59:35 -070039 * finish off updating the recorded status of a file after a successful
40 * operation completion
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * - starts callback expiry timer
42 * - adds to server's callback list
43 */
David Howells260a9802007-04-26 15:59:35 -070044void afs_vnode_finalise_status_update(struct afs_vnode *vnode,
45 struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
David Howells260a9802007-04-26 15:59:35 -070047 spin_lock(&vnode->lock);
David Howells260a9802007-04-26 15:59:35 -070048 vnode->update_cnt--;
49 ASSERTCMP(vnode->update_cnt, >=, 0);
50 spin_unlock(&vnode->lock);
51
52 wake_up_all(&vnode->update_waitq);
David Howells260a9802007-04-26 15:59:35 -070053 _leave("");
54}
55
56/*
57 * finish off updating the recorded status of a file after an operation failed
58 */
David Howells8b2a4642017-11-02 15:27:50 +000059static void afs_vnode_status_update_failed(struct afs_fs_cursor *fc,
60 struct afs_vnode *vnode)
David Howells260a9802007-04-26 15:59:35 -070061{
David Howells8b2a4642017-11-02 15:27:50 +000062 _enter("{%x:%u},%d", vnode->fid.vid, vnode->fid.vnode, fc->ac.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 spin_lock(&vnode->lock);
65
David Howells8b2a4642017-11-02 15:27:50 +000066 if (fc->ac.error == -ENOENT) {
David Howells08e0e7c2007-04-26 15:55:03 -070067 /* the file was deleted on the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 _debug("got NOENT from server - marking file deleted");
David Howells08e0e7c2007-04-26 15:55:03 -070069 afs_vnode_deleted_remotely(vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 vnode->update_cnt--;
David Howells260a9802007-04-26 15:59:35 -070073 ASSERTCMP(vnode->update_cnt, >=, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 spin_unlock(&vnode->lock);
75
76 wake_up_all(&vnode->update_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 _leave("");
David Howellsec268152007-04-26 15:49:28 -070078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * fetch file status from the volume
82 * - don't issue a fetch if:
83 * - the changed bit is not set and there's a valid callback
84 * - there are any outstanding ops that will fetch the status
85 * - TODO implement local caching
86 */
David Howellsbe080a62017-11-02 15:27:49 +000087int afs_vnode_fetch_status(struct afs_vnode *vnode, struct key *key, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
David Howells8b2a4642017-11-02 15:27:50 +000089 struct afs_fs_cursor fc;
David Howellsbe080a62017-11-02 15:27:49 +000090 unsigned int cb_break = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 DECLARE_WAITQUEUE(myself, current);
93
David Howellsc435ee32017-11-02 15:27:49 +000094 _enter("%s,{%x:%u.%u,S=%lx},%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 vnode->volume->vlocation->vldb.name,
David Howellsc435ee32017-11-02 15:27:49 +000096 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
97 vnode->flags,
98 force);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Howellsc435ee32017-11-02 15:27:49 +0000100 if (!force && test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 _leave(" [unchanged]");
102 return 0;
103 }
104
David Howells08e0e7c2007-04-26 15:55:03 -0700105 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 _leave(" [deleted]");
107 return -ENOENT;
108 }
109
David Howellsbe080a62017-11-02 15:27:49 +0000110 cb_break = vnode->cb_break + vnode->cb_s_break;
David Howells00d3b7a2007-04-26 15:57:07 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_lock(&vnode->lock);
113
David Howellsc435ee32017-11-02 15:27:49 +0000114 if (!force && test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_unlock(&vnode->lock);
116 _leave(" [unchanged]");
117 return 0;
118 }
119
David Howells260a9802007-04-26 15:59:35 -0700120 ASSERTCMP(vnode->update_cnt, >=, 0);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (vnode->update_cnt > 0) {
123 /* someone else started a fetch */
David Howells260a9802007-04-26 15:59:35 -0700124 _debug("wait on fetch %d", vnode->update_cnt);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700127 ASSERT(myself.func != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 add_wait_queue(&vnode->update_waitq, &myself);
129
130 /* wait for the status to be updated */
131 for (;;) {
David Howellsc435ee32017-11-02 15:27:49 +0000132 if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700134 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 break;
136
David Howells08e0e7c2007-04-26 15:55:03 -0700137 /* check to see if it got updated and invalidated all
138 * before we saw it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (vnode->update_cnt == 0) {
140 remove_wait_queue(&vnode->update_waitq,
141 &myself);
142 set_current_state(TASK_RUNNING);
143 goto get_anyway;
144 }
145
146 spin_unlock(&vnode->lock);
147
148 schedule();
149 set_current_state(TASK_UNINTERRUPTIBLE);
150
151 spin_lock(&vnode->lock);
152 }
153
154 remove_wait_queue(&vnode->update_waitq, &myself);
155 spin_unlock(&vnode->lock);
156 set_current_state(TASK_RUNNING);
157
David Howells08e0e7c2007-04-26 15:55:03 -0700158 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ?
159 -ENOENT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
David Howellsec268152007-04-26 15:49:28 -0700162get_anyway:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* okay... we're going to have to initiate the op */
164 vnode->update_cnt++;
165
166 spin_unlock(&vnode->lock);
167
168 /* merge AFS status fetches and clear outstanding callback on this
169 * vnode */
David Howells8b2a4642017-11-02 15:27:50 +0000170 afs_init_fs_cursor(&fc, vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 do {
172 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000173 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells260a9802007-04-26 15:59:35 -0700174 goto no_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Howells8b2a4642017-11-02 15:27:50 +0000176 fc.ac.error = afs_fs_fetch_file_status(&fc, key, vnode, NULL, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
David Howells8b2a4642017-11-02 15:27:50 +0000178 } while (afs_iterate_fs_cursor(&fc, vnode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000181 if (fc.ac.error == 0) {
David Howells260a9802007-04-26 15:59:35 -0700182 _debug("adjust");
David Howellsbe080a62017-11-02 15:27:49 +0000183 afs_cache_permit(vnode, key, cb_break);
David Howells8b2a4642017-11-02 15:27:50 +0000184 afs_vnode_finalise_status_update(vnode, fc.server);
David Howells260a9802007-04-26 15:59:35 -0700185 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000186 _debug("failed [%d]", fc.ac.error);
187 afs_vnode_status_update_failed(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David Howells8b2a4642017-11-02 15:27:50 +0000190out:
191 afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700192 ASSERTCMP(vnode->update_cnt, >=, 0);
David Howells8b2a4642017-11-02 15:27:50 +0000193 _leave(" = %d [cnt %d]", fc.ac.error, vnode->update_cnt);
194 return fc.ac.error;
David Howells260a9802007-04-26 15:59:35 -0700195
196no_server:
197 spin_lock(&vnode->lock);
198 vnode->update_cnt--;
David Howells260a9802007-04-26 15:59:35 -0700199 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000200 goto out;
David Howellsec268152007-04-26 15:49:28 -0700201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
204 * fetch file data from the volume
David Howells260a9802007-04-26 15:59:35 -0700205 * - TODO implement caching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
David Howells00d3b7a2007-04-26 15:57:07 -0700207int afs_vnode_fetch_data(struct afs_vnode *vnode, struct key *key,
David Howells196ee9c2017-01-05 10:38:34 +0000208 struct afs_read *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
David Howells8b2a4642017-11-02 15:27:50 +0000210 struct afs_fs_cursor fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Howells416351f2007-05-09 02:33:45 -0700212 _enter("%s{%x:%u.%u},%x,,,",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 vnode->volume->vlocation->vldb.name,
214 vnode->fid.vid,
215 vnode->fid.vnode,
David Howells00d3b7a2007-04-26 15:57:07 -0700216 vnode->fid.unique,
217 key_serial(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* this op will fetch the status */
220 spin_lock(&vnode->lock);
221 vnode->update_cnt++;
222 spin_unlock(&vnode->lock);
223
224 /* merge in AFS status fetches and clear outstanding callback on this
225 * vnode */
David Howells8b2a4642017-11-02 15:27:50 +0000226 afs_init_fs_cursor(&fc, vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 do {
228 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000229 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells260a9802007-04-26 15:59:35 -0700230 goto no_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
David Howells8b2a4642017-11-02 15:27:50 +0000232 fc.ac.error = afs_fs_fetch_data(&fc, key, vnode, desc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
David Howells8b2a4642017-11-02 15:27:50 +0000234 } while (afs_iterate_fs_cursor(&fc, vnode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000237 if (fc.ac.error == 0)
238 afs_vnode_finalise_status_update(vnode, fc.server);
239 else
240 afs_vnode_status_update_failed(&fc, vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
David Howells8b2a4642017-11-02 15:27:50 +0000242out:
243 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700244
245no_server:
246 spin_lock(&vnode->lock);
247 vnode->update_cnt--;
248 ASSERTCMP(vnode->update_cnt, >=, 0);
249 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000250 goto out;
David Howells260a9802007-04-26 15:59:35 -0700251}
252
253/*
254 * make a file or a directory
255 */
256int afs_vnode_create(struct afs_vnode *vnode, struct key *key,
257 const char *name, umode_t mode, struct afs_fid *newfid,
258 struct afs_file_status *newstatus,
259 struct afs_callback *newcb, struct afs_server **_server)
260{
David Howells8b2a4642017-11-02 15:27:50 +0000261 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -0700262
David Howells416351f2007-05-09 02:33:45 -0700263 _enter("%s{%x:%u.%u},%x,%s,,",
David Howells260a9802007-04-26 15:59:35 -0700264 vnode->volume->vlocation->vldb.name,
265 vnode->fid.vid,
266 vnode->fid.vnode,
267 vnode->fid.unique,
268 key_serial(key),
269 name);
270
271 /* this op will fetch the status on the directory we're creating in */
272 spin_lock(&vnode->lock);
273 vnode->update_cnt++;
274 spin_unlock(&vnode->lock);
275
David Howells8b2a4642017-11-02 15:27:50 +0000276 afs_init_fs_cursor(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700277 do {
278 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000279 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells260a9802007-04-26 15:59:35 -0700280 goto no_server;
281
David Howells8b2a4642017-11-02 15:27:50 +0000282 fc.ac.error = afs_fs_create(&fc, key, vnode, name, mode, newfid,
283 newstatus, newcb, false);
David Howells260a9802007-04-26 15:59:35 -0700284
David Howells8b2a4642017-11-02 15:27:50 +0000285 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells260a9802007-04-26 15:59:35 -0700286
287 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000288 if (fc.ac.error == 0) {
289 afs_vnode_finalise_status_update(vnode, fc.server);
290 *_server = fc.server;
291 fc.server = NULL;
David Howells260a9802007-04-26 15:59:35 -0700292 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000293 afs_vnode_status_update_failed(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700294 *_server = NULL;
295 }
296
David Howells8b2a4642017-11-02 15:27:50 +0000297out:
298 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700299
300no_server:
301 spin_lock(&vnode->lock);
302 vnode->update_cnt--;
303 ASSERTCMP(vnode->update_cnt, >=, 0);
304 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000305 goto out;
David Howells260a9802007-04-26 15:59:35 -0700306}
307
308/*
309 * remove a file or directory
310 */
311int afs_vnode_remove(struct afs_vnode *vnode, struct key *key, const char *name,
312 bool isdir)
313{
David Howells8b2a4642017-11-02 15:27:50 +0000314 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -0700315
David Howells416351f2007-05-09 02:33:45 -0700316 _enter("%s{%x:%u.%u},%x,%s",
David Howells260a9802007-04-26 15:59:35 -0700317 vnode->volume->vlocation->vldb.name,
318 vnode->fid.vid,
319 vnode->fid.vnode,
320 vnode->fid.unique,
321 key_serial(key),
322 name);
323
324 /* this op will fetch the status on the directory we're removing from */
325 spin_lock(&vnode->lock);
326 vnode->update_cnt++;
327 spin_unlock(&vnode->lock);
328
David Howells8b2a4642017-11-02 15:27:50 +0000329 afs_init_fs_cursor(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700330 do {
331 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000332 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells260a9802007-04-26 15:59:35 -0700333 goto no_server;
334
David Howells8b2a4642017-11-02 15:27:50 +0000335 fc.ac.error = afs_fs_remove(&fc, key, vnode, name, isdir, false);
David Howells260a9802007-04-26 15:59:35 -0700336
David Howells8b2a4642017-11-02 15:27:50 +0000337 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells260a9802007-04-26 15:59:35 -0700338
339 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000340 if (fc.ac.error == 0)
341 afs_vnode_finalise_status_update(vnode, fc.server);
342 else
343 afs_vnode_status_update_failed(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700344
David Howells8b2a4642017-11-02 15:27:50 +0000345out:
346 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700347
348no_server:
349 spin_lock(&vnode->lock);
350 vnode->update_cnt--;
351 ASSERTCMP(vnode->update_cnt, >=, 0);
352 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000353 goto out;
David Howells260a9802007-04-26 15:59:35 -0700354}
355
356/*
357 * create a hard link
358 */
Randy Dunlap03a9c302007-07-15 23:40:16 -0700359int afs_vnode_link(struct afs_vnode *dvnode, struct afs_vnode *vnode,
David Howells260a9802007-04-26 15:59:35 -0700360 struct key *key, const char *name)
361{
David Howells8b2a4642017-11-02 15:27:50 +0000362 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -0700363
David Howells416351f2007-05-09 02:33:45 -0700364 _enter("%s{%x:%u.%u},%s{%x:%u.%u},%x,%s",
David Howells260a9802007-04-26 15:59:35 -0700365 dvnode->volume->vlocation->vldb.name,
366 dvnode->fid.vid,
367 dvnode->fid.vnode,
368 dvnode->fid.unique,
369 vnode->volume->vlocation->vldb.name,
370 vnode->fid.vid,
371 vnode->fid.vnode,
372 vnode->fid.unique,
373 key_serial(key),
374 name);
375
376 /* this op will fetch the status on the directory we're removing from */
377 spin_lock(&vnode->lock);
378 vnode->update_cnt++;
379 spin_unlock(&vnode->lock);
380 spin_lock(&dvnode->lock);
381 dvnode->update_cnt++;
382 spin_unlock(&dvnode->lock);
383
David Howells8b2a4642017-11-02 15:27:50 +0000384 afs_init_fs_cursor(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700385 do {
386 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000387 if (!afs_volume_pick_fileserver(&fc, dvnode))
David Howells260a9802007-04-26 15:59:35 -0700388 goto no_server;
389
David Howells8b2a4642017-11-02 15:27:50 +0000390 fc.ac.error = afs_fs_link(&fc, key, dvnode, vnode, name, false);
David Howells260a9802007-04-26 15:59:35 -0700391
David Howells8b2a4642017-11-02 15:27:50 +0000392 } while (afs_iterate_fs_cursor(&fc, dvnode));
David Howells260a9802007-04-26 15:59:35 -0700393
394 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000395 if (fc.ac.error == 0) {
396 afs_vnode_finalise_status_update(vnode, fc.server);
397 afs_vnode_finalise_status_update(dvnode, fc.server);
David Howells260a9802007-04-26 15:59:35 -0700398 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000399 afs_vnode_status_update_failed(&fc, vnode);
400 afs_vnode_status_update_failed(&fc, dvnode);
David Howells260a9802007-04-26 15:59:35 -0700401 }
402
David Howells8b2a4642017-11-02 15:27:50 +0000403out:
404 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700405
406no_server:
407 spin_lock(&vnode->lock);
408 vnode->update_cnt--;
409 ASSERTCMP(vnode->update_cnt, >=, 0);
410 spin_unlock(&vnode->lock);
411 spin_lock(&dvnode->lock);
412 dvnode->update_cnt--;
413 ASSERTCMP(dvnode->update_cnt, >=, 0);
414 spin_unlock(&dvnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000415 goto out;
David Howells260a9802007-04-26 15:59:35 -0700416}
417
418/*
419 * create a symbolic link
420 */
421int afs_vnode_symlink(struct afs_vnode *vnode, struct key *key,
422 const char *name, const char *content,
423 struct afs_fid *newfid,
424 struct afs_file_status *newstatus,
425 struct afs_server **_server)
426{
David Howells8b2a4642017-11-02 15:27:50 +0000427 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -0700428
David Howells416351f2007-05-09 02:33:45 -0700429 _enter("%s{%x:%u.%u},%x,%s,%s,,,",
David Howells260a9802007-04-26 15:59:35 -0700430 vnode->volume->vlocation->vldb.name,
431 vnode->fid.vid,
432 vnode->fid.vnode,
433 vnode->fid.unique,
434 key_serial(key),
435 name, content);
436
437 /* this op will fetch the status on the directory we're creating in */
438 spin_lock(&vnode->lock);
439 vnode->update_cnt++;
440 spin_unlock(&vnode->lock);
441
David Howells8b2a4642017-11-02 15:27:50 +0000442 afs_init_fs_cursor(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700443 do {
444 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000445 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells260a9802007-04-26 15:59:35 -0700446 goto no_server;
447
David Howells8b2a4642017-11-02 15:27:50 +0000448 fc.ac.error = afs_fs_symlink(&fc, key, vnode, name, content,
449 newfid, newstatus, false);
David Howells260a9802007-04-26 15:59:35 -0700450
David Howells8b2a4642017-11-02 15:27:50 +0000451 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells260a9802007-04-26 15:59:35 -0700452
453 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000454 if (fc.ac.error == 0) {
455 afs_vnode_finalise_status_update(vnode, fc.server);
456 *_server = fc.server;
457 fc.server = NULL;
David Howells260a9802007-04-26 15:59:35 -0700458 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000459 afs_vnode_status_update_failed(&fc, vnode);
David Howells260a9802007-04-26 15:59:35 -0700460 *_server = NULL;
461 }
462
David Howells8b2a4642017-11-02 15:27:50 +0000463out:
464 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells260a9802007-04-26 15:59:35 -0700465
466no_server:
467 spin_lock(&vnode->lock);
468 vnode->update_cnt--;
469 ASSERTCMP(vnode->update_cnt, >=, 0);
470 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000471 *_server = NULL;
472 goto out;
David Howells260a9802007-04-26 15:59:35 -0700473}
474
475/*
476 * rename a file
477 */
478int afs_vnode_rename(struct afs_vnode *orig_dvnode,
479 struct afs_vnode *new_dvnode,
480 struct key *key,
481 const char *orig_name,
482 const char *new_name)
483{
David Howells8b2a4642017-11-02 15:27:50 +0000484 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -0700485
David Howells416351f2007-05-09 02:33:45 -0700486 _enter("%s{%x:%u.%u},%s{%u,%u,%u},%x,%s,%s",
David Howells260a9802007-04-26 15:59:35 -0700487 orig_dvnode->volume->vlocation->vldb.name,
488 orig_dvnode->fid.vid,
489 orig_dvnode->fid.vnode,
490 orig_dvnode->fid.unique,
491 new_dvnode->volume->vlocation->vldb.name,
492 new_dvnode->fid.vid,
493 new_dvnode->fid.vnode,
494 new_dvnode->fid.unique,
495 key_serial(key),
496 orig_name,
497 new_name);
498
499 /* this op will fetch the status on both the directories we're dealing
500 * with */
501 spin_lock(&orig_dvnode->lock);
502 orig_dvnode->update_cnt++;
503 spin_unlock(&orig_dvnode->lock);
504 if (new_dvnode != orig_dvnode) {
505 spin_lock(&new_dvnode->lock);
506 new_dvnode->update_cnt++;
507 spin_unlock(&new_dvnode->lock);
508 }
509
David Howells8b2a4642017-11-02 15:27:50 +0000510 afs_init_fs_cursor(&fc, orig_dvnode);
David Howells260a9802007-04-26 15:59:35 -0700511 do {
512 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000513 if (!afs_volume_pick_fileserver(&fc, orig_dvnode))
David Howells260a9802007-04-26 15:59:35 -0700514 goto no_server;
515
David Howells8b2a4642017-11-02 15:27:50 +0000516 fc.ac.error = afs_fs_rename(&fc, key, orig_dvnode, orig_name,
517 new_dvnode, new_name, false);
David Howells260a9802007-04-26 15:59:35 -0700518
David Howells8b2a4642017-11-02 15:27:50 +0000519 } while (afs_iterate_fs_cursor(&fc, orig_dvnode));
David Howells260a9802007-04-26 15:59:35 -0700520
521 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000522 if (fc.ac.error == 0) {
523 afs_vnode_finalise_status_update(orig_dvnode, fc.server);
David Howells260a9802007-04-26 15:59:35 -0700524 if (new_dvnode != orig_dvnode)
David Howells8b2a4642017-11-02 15:27:50 +0000525 afs_vnode_finalise_status_update(new_dvnode, fc.server);
David Howells260a9802007-04-26 15:59:35 -0700526 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000527 afs_vnode_status_update_failed(&fc, orig_dvnode);
David Howells260a9802007-04-26 15:59:35 -0700528 if (new_dvnode != orig_dvnode)
David Howells8b2a4642017-11-02 15:27:50 +0000529 afs_vnode_status_update_failed(&fc, new_dvnode);
David Howells260a9802007-04-26 15:59:35 -0700530 }
531
David Howells8b2a4642017-11-02 15:27:50 +0000532out:
533 return afs_end_fs_cursor(&fc, afs_v2net(orig_dvnode));
David Howells260a9802007-04-26 15:59:35 -0700534
535no_server:
536 spin_lock(&orig_dvnode->lock);
537 orig_dvnode->update_cnt--;
538 ASSERTCMP(orig_dvnode->update_cnt, >=, 0);
539 spin_unlock(&orig_dvnode->lock);
540 if (new_dvnode != orig_dvnode) {
541 spin_lock(&new_dvnode->lock);
542 new_dvnode->update_cnt--;
543 ASSERTCMP(new_dvnode->update_cnt, >=, 0);
544 spin_unlock(&new_dvnode->lock);
545 }
David Howells8b2a4642017-11-02 15:27:50 +0000546 goto out;
David Howellsec268152007-04-26 15:49:28 -0700547}
David Howells31143d52007-05-09 02:33:46 -0700548
549/*
550 * write to a file
551 */
552int afs_vnode_store_data(struct afs_writeback *wb, pgoff_t first, pgoff_t last,
553 unsigned offset, unsigned to)
554{
David Howells8b2a4642017-11-02 15:27:50 +0000555 struct afs_fs_cursor fc;
David Howells31143d52007-05-09 02:33:46 -0700556 struct afs_vnode *vnode = wb->vnode;
David Howells31143d52007-05-09 02:33:46 -0700557
558 _enter("%s{%x:%u.%u},%x,%lx,%lx,%x,%x",
559 vnode->volume->vlocation->vldb.name,
560 vnode->fid.vid,
561 vnode->fid.vnode,
562 vnode->fid.unique,
563 key_serial(wb->key),
564 first, last, offset, to);
565
566 /* this op will fetch the status */
567 spin_lock(&vnode->lock);
568 vnode->update_cnt++;
569 spin_unlock(&vnode->lock);
570
David Howells8b2a4642017-11-02 15:27:50 +0000571 afs_init_fs_cursor(&fc, vnode);
David Howells31143d52007-05-09 02:33:46 -0700572 do {
573 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000574 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells31143d52007-05-09 02:33:46 -0700575 goto no_server;
576
David Howells8b2a4642017-11-02 15:27:50 +0000577 fc.ac.error = afs_fs_store_data(&fc, wb, first, last, offset, to,
578 false);
David Howells31143d52007-05-09 02:33:46 -0700579
David Howells8b2a4642017-11-02 15:27:50 +0000580 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells31143d52007-05-09 02:33:46 -0700581
582 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000583 if (fc.ac.error == 0) {
584 afs_vnode_finalise_status_update(vnode, fc.server);
David Howells31143d52007-05-09 02:33:46 -0700585 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000586 afs_vnode_status_update_failed(&fc, vnode);
David Howells31143d52007-05-09 02:33:46 -0700587 }
588
David Howells8b2a4642017-11-02 15:27:50 +0000589out:
590 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells31143d52007-05-09 02:33:46 -0700591
592no_server:
593 spin_lock(&vnode->lock);
594 vnode->update_cnt--;
595 ASSERTCMP(vnode->update_cnt, >=, 0);
596 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000597 goto out;
David Howells31143d52007-05-09 02:33:46 -0700598}
599
600/*
601 * set the attributes on a file
602 */
603int afs_vnode_setattr(struct afs_vnode *vnode, struct key *key,
604 struct iattr *attr)
605{
David Howells8b2a4642017-11-02 15:27:50 +0000606 struct afs_fs_cursor fc;
David Howells31143d52007-05-09 02:33:46 -0700607
608 _enter("%s{%x:%u.%u},%x",
609 vnode->volume->vlocation->vldb.name,
610 vnode->fid.vid,
611 vnode->fid.vnode,
612 vnode->fid.unique,
613 key_serial(key));
614
615 /* this op will fetch the status */
616 spin_lock(&vnode->lock);
617 vnode->update_cnt++;
618 spin_unlock(&vnode->lock);
619
David Howells8b2a4642017-11-02 15:27:50 +0000620 afs_init_fs_cursor(&fc, vnode);
David Howells31143d52007-05-09 02:33:46 -0700621 do {
622 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000623 if (!afs_volume_pick_fileserver(&fc, vnode))
David Howells31143d52007-05-09 02:33:46 -0700624 goto no_server;
625
David Howells8b2a4642017-11-02 15:27:50 +0000626 fc.ac.error = afs_fs_setattr(&fc, key, vnode, attr, false);
David Howells31143d52007-05-09 02:33:46 -0700627
David Howells8b2a4642017-11-02 15:27:50 +0000628 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells31143d52007-05-09 02:33:46 -0700629
630 /* adjust the flags */
David Howells8b2a4642017-11-02 15:27:50 +0000631 if (fc.ac.error == 0) {
632 afs_vnode_finalise_status_update(vnode, fc.server);
David Howells31143d52007-05-09 02:33:46 -0700633 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000634 afs_vnode_status_update_failed(&fc, vnode);
David Howells31143d52007-05-09 02:33:46 -0700635 }
636
David Howells8b2a4642017-11-02 15:27:50 +0000637out:
638 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells31143d52007-05-09 02:33:46 -0700639
640no_server:
641 spin_lock(&vnode->lock);
642 vnode->update_cnt--;
643 ASSERTCMP(vnode->update_cnt, >=, 0);
644 spin_unlock(&vnode->lock);
David Howells8b2a4642017-11-02 15:27:50 +0000645 goto out;
David Howells31143d52007-05-09 02:33:46 -0700646}
David Howells45222b92007-05-10 22:22:20 -0700647
648/*
649 * get the status of a volume
650 */
651int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key,
652 struct afs_volume_status *vs)
653{
David Howells8b2a4642017-11-02 15:27:50 +0000654 struct afs_fs_cursor fc;
David Howells45222b92007-05-10 22:22:20 -0700655
656 _enter("%s{%x:%u.%u},%x,",
657 vnode->volume->vlocation->vldb.name,
658 vnode->fid.vid,
659 vnode->fid.vnode,
660 vnode->fid.unique,
661 key_serial(key));
662
David Howells8b2a4642017-11-02 15:27:50 +0000663 afs_init_fs_cursor(&fc, vnode);
David Howells45222b92007-05-10 22:22:20 -0700664 do {
665 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000666 if (!afs_volume_pick_fileserver(&fc, vnode))
667 break;
David Howells45222b92007-05-10 22:22:20 -0700668
David Howells8b2a4642017-11-02 15:27:50 +0000669 fc.ac.error = afs_fs_get_volume_status(&fc, key, vnode, vs, false);
David Howells45222b92007-05-10 22:22:20 -0700670
David Howells8b2a4642017-11-02 15:27:50 +0000671 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howells45222b92007-05-10 22:22:20 -0700672
David Howells8b2a4642017-11-02 15:27:50 +0000673 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howellse8d6c552007-07-15 23:40:12 -0700674}
675
676/*
677 * get a lock on a file
678 */
679int afs_vnode_set_lock(struct afs_vnode *vnode, struct key *key,
680 afs_lock_type_t type)
681{
David Howells8b2a4642017-11-02 15:27:50 +0000682 struct afs_fs_cursor fc;
David Howellse8d6c552007-07-15 23:40:12 -0700683
684 _enter("%s{%x:%u.%u},%x,%u",
685 vnode->volume->vlocation->vldb.name,
686 vnode->fid.vid,
687 vnode->fid.vnode,
688 vnode->fid.unique,
689 key_serial(key), type);
690
David Howells8b2a4642017-11-02 15:27:50 +0000691 afs_init_fs_cursor(&fc, vnode);
David Howellse8d6c552007-07-15 23:40:12 -0700692 do {
693 /* pick a server to query */
David Howells8b2a4642017-11-02 15:27:50 +0000694 if (!afs_volume_pick_fileserver(&fc, vnode))
695 break;
David Howellse8d6c552007-07-15 23:40:12 -0700696
David Howells8b2a4642017-11-02 15:27:50 +0000697 fc.ac.error = afs_fs_set_lock(&fc, key, vnode, type, false);
David Howellse8d6c552007-07-15 23:40:12 -0700698
David Howells8b2a4642017-11-02 15:27:50 +0000699 } while (afs_iterate_fs_cursor(&fc, vnode));
David Howellse8d6c552007-07-15 23:40:12 -0700700
David Howells8b2a4642017-11-02 15:27:50 +0000701 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howellse8d6c552007-07-15 23:40:12 -0700702}
703
704/*
705 * extend a lock on a file
706 */
707int afs_vnode_extend_lock(struct afs_vnode *vnode, struct key *key)
708{
David Howells8b2a4642017-11-02 15:27:50 +0000709 struct afs_fs_cursor fc;
David Howellse8d6c552007-07-15 23:40:12 -0700710 int ret;
711
712 _enter("%s{%x:%u.%u},%x",
713 vnode->volume->vlocation->vldb.name,
714 vnode->fid.vid,
715 vnode->fid.vnode,
716 vnode->fid.unique,
717 key_serial(key));
718
David Howells8b2a4642017-11-02 15:27:50 +0000719 ret = afs_set_fs_cursor(&fc, vnode);
720 if (ret < 0)
721 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700722
David Howells8b2a4642017-11-02 15:27:50 +0000723 fc.ac.error = afs_fs_extend_lock(&fc, key, vnode, false);
David Howellse8d6c552007-07-15 23:40:12 -0700724
David Howells8b2a4642017-11-02 15:27:50 +0000725 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howellse8d6c552007-07-15 23:40:12 -0700726}
727
728/*
729 * release a lock on a file
730 */
731int afs_vnode_release_lock(struct afs_vnode *vnode, struct key *key)
732{
David Howells8b2a4642017-11-02 15:27:50 +0000733 struct afs_fs_cursor fc;
David Howellse8d6c552007-07-15 23:40:12 -0700734 int ret;
735
736 _enter("%s{%x:%u.%u},%x",
737 vnode->volume->vlocation->vldb.name,
738 vnode->fid.vid,
739 vnode->fid.vnode,
740 vnode->fid.unique,
741 key_serial(key));
742
David Howells8b2a4642017-11-02 15:27:50 +0000743 ret = afs_set_fs_cursor(&fc, vnode);
744 if (ret < 0)
745 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700746
David Howells8b2a4642017-11-02 15:27:50 +0000747 fc.ac.error = afs_fs_release_lock(&fc, key, vnode, false);
David Howellse8d6c552007-07-15 23:40:12 -0700748
David Howells8b2a4642017-11-02 15:27:50 +0000749 return afs_end_fs_cursor(&fc, afs_v2net(vnode));
David Howells45222b92007-05-10 22:22:20 -0700750}