afs: Clear AFS_VNODE_CB_PROMISED if we detect callback expiry
[linux-2.6-microblaze.git] / fs / afs / inode.c
1 /*
2  * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3  *
4  * This software may be freely redistributed under the terms of the
5  * GNU General Public License.
6  *
7  * You should have received a copy of the GNU General Public License
8  * along with this program; if not, write to the Free Software
9  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10  *
11  * Authors: David Woodhouse <dwmw2@infradead.org>
12  *          David Howells <dhowells@redhat.com>
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/pagemap.h>
21 #include <linux/sched.h>
22 #include <linux/mount.h>
23 #include <linux/namei.h>
24 #include <linux/iversion.h>
25 #include "internal.h"
26
27 static const struct inode_operations afs_symlink_inode_operations = {
28         .get_link       = page_get_link,
29         .listxattr      = afs_listxattr,
30 };
31
32 static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
33 {
34         static unsigned long once_only;
35
36         pr_warn("kAFS: AFS vnode with undefined type %u\n",
37                 vnode->status.type);
38         pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
39                 vnode->status.abort_code,
40                 vnode->status.mode,
41                 vnode->status.size,
42                 vnode->status.data_version);
43         pr_warn("kAFS: vnode %llx:%llx:%x\n",
44                 vnode->fid.vid,
45                 vnode->fid.vnode,
46                 vnode->fid.unique);
47         if (parent_vnode)
48                 pr_warn("kAFS: dir %llx:%llx:%x\n",
49                         parent_vnode->fid.vid,
50                         parent_vnode->fid.vnode,
51                         parent_vnode->fid.unique);
52
53         if (!test_and_set_bit(0, &once_only))
54                 dump_stack();
55 }
56
57 /*
58  * Initialise an inode from the vnode status.
59  */
60 static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key,
61                                       struct afs_cb_interest *cbi,
62                                       struct afs_vnode *parent_vnode,
63                                       struct afs_status_cb *scb)
64 {
65         struct afs_cb_interest *old_cbi = NULL;
66         struct afs_file_status *status = &scb->status;
67         struct inode *inode = AFS_VNODE_TO_I(vnode);
68         struct timespec64 t;
69
70         _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
71                status->type,
72                status->nlink,
73                (unsigned long long) status->size,
74                status->data_version,
75                status->mode);
76
77         write_seqlock(&vnode->cb_lock);
78
79         vnode->status = *status;
80
81         t = status->mtime_client;
82         inode->i_ctime = t;
83         inode->i_mtime = t;
84         inode->i_atime = t;
85         inode->i_uid = make_kuid(&init_user_ns, status->owner);
86         inode->i_gid = make_kgid(&init_user_ns, status->group);
87         set_nlink(&vnode->vfs_inode, status->nlink);
88
89         switch (status->type) {
90         case AFS_FTYPE_FILE:
91                 inode->i_mode   = S_IFREG | status->mode;
92                 inode->i_op     = &afs_file_inode_operations;
93                 inode->i_fop    = &afs_file_operations;
94                 inode->i_mapping->a_ops = &afs_fs_aops;
95                 break;
96         case AFS_FTYPE_DIR:
97                 inode->i_mode   = S_IFDIR | status->mode;
98                 inode->i_op     = &afs_dir_inode_operations;
99                 inode->i_fop    = &afs_dir_file_operations;
100                 inode->i_mapping->a_ops = &afs_dir_aops;
101                 break;
102         case AFS_FTYPE_SYMLINK:
103                 /* Symlinks with a mode of 0644 are actually mountpoints. */
104                 if ((status->mode & 0777) == 0644) {
105                         inode->i_flags |= S_AUTOMOUNT;
106
107                         set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
108
109                         inode->i_mode   = S_IFDIR | 0555;
110                         inode->i_op     = &afs_mntpt_inode_operations;
111                         inode->i_fop    = &afs_mntpt_file_operations;
112                         inode->i_mapping->a_ops = &afs_fs_aops;
113                 } else {
114                         inode->i_mode   = S_IFLNK | status->mode;
115                         inode->i_op     = &afs_symlink_inode_operations;
116                         inode->i_mapping->a_ops = &afs_fs_aops;
117                 }
118                 inode_nohighmem(inode);
119                 break;
120         default:
121                 dump_vnode(vnode, parent_vnode);
122                 write_sequnlock(&vnode->cb_lock);
123                 return afs_protocol_error(NULL, -EBADMSG, afs_eproto_file_type);
124         }
125
126         /*
127          * Estimate 512 bytes  blocks used, rounded up to nearest 1K
128          * for consistency with other AFS clients.
129          */
130         inode->i_blocks         = ((i_size_read(inode) + 1023) >> 10) << 1;
131         i_size_write(&vnode->vfs_inode, status->size);
132
133         vnode->invalid_before   = status->data_version;
134         inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
135
136         if (!scb->have_cb) {
137                 /* it's a symlink we just created (the fileserver
138                  * didn't give us a callback) */
139                 vnode->cb_expires_at = ktime_get_real_seconds();
140         } else {
141                 vnode->cb_expires_at = scb->callback.expires_at;
142                 old_cbi = rcu_dereference_protected(vnode->cb_interest,
143                                                     lockdep_is_held(&vnode->cb_lock.lock));
144                 if (cbi != old_cbi)
145                         rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(cbi));
146                 else
147                         old_cbi = NULL;
148                 set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
149         }
150
151         write_sequnlock(&vnode->cb_lock);
152         afs_put_cb_interest(afs_v2net(vnode), old_cbi);
153         return 0;
154 }
155
156 /*
157  * Update the core inode struct from a returned status record.
158  */
159 static void afs_apply_status(struct afs_fs_cursor *fc,
160                              struct afs_vnode *vnode,
161                              struct afs_status_cb *scb,
162                              const afs_dataversion_t *expected_version)
163 {
164         struct afs_file_status *status = &scb->status;
165         struct timespec64 t;
166         umode_t mode;
167         bool data_changed = false;
168
169         BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
170
171         if (status->type != vnode->status.type) {
172                 pr_warning("Vnode %llx:%llx:%x changed type %u to %u\n",
173                            vnode->fid.vid,
174                            vnode->fid.vnode,
175                            vnode->fid.unique,
176                            status->type, vnode->status.type);
177                 afs_protocol_error(NULL, -EBADMSG, afs_eproto_bad_status);
178                 return;
179         }
180
181         if (status->nlink != vnode->status.nlink)
182                 set_nlink(&vnode->vfs_inode, status->nlink);
183
184         if (status->owner != vnode->status.owner)
185                 vnode->vfs_inode.i_uid = make_kuid(&init_user_ns, status->owner);
186
187         if (status->group != vnode->status.group)
188                 vnode->vfs_inode.i_gid = make_kgid(&init_user_ns, status->group);
189
190         if (status->mode != vnode->status.mode) {
191                 mode = vnode->vfs_inode.i_mode;
192                 mode &= ~S_IALLUGO;
193                 mode |= status->mode;
194                 WRITE_ONCE(vnode->vfs_inode.i_mode, mode);
195         }
196
197         t = status->mtime_client;
198         vnode->vfs_inode.i_ctime = t;
199         vnode->vfs_inode.i_mtime = t;
200         vnode->vfs_inode.i_atime = t;
201
202         if (vnode->status.data_version != status->data_version)
203                 data_changed = true;
204
205         vnode->status = *status;
206
207         if (expected_version &&
208             *expected_version != status->data_version) {
209                 kdebug("vnode modified %llx on {%llx:%llu} [exp %llx] %s",
210                        (unsigned long long) status->data_version,
211                        vnode->fid.vid, vnode->fid.vnode,
212                        (unsigned long long) *expected_version,
213                        fc->type ? fc->type->name : "???");
214                 vnode->invalid_before = status->data_version;
215                 if (vnode->status.type == AFS_FTYPE_DIR) {
216                         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
217                                 afs_stat_v(vnode, n_inval);
218                 } else {
219                         set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
220                 }
221         } else if (vnode->status.type == AFS_FTYPE_DIR) {
222                 /* Expected directory change is handled elsewhere so
223                  * that we can locally edit the directory and save on a
224                  * download.
225                  */
226                 if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
227                         data_changed = false;
228         }
229
230         if (data_changed) {
231                 inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
232                 i_size_write(&vnode->vfs_inode, status->size);
233         }
234 }
235
236 /*
237  * Apply a callback to a vnode.
238  */
239 static void afs_apply_callback(struct afs_fs_cursor *fc,
240                                struct afs_vnode *vnode,
241                                struct afs_status_cb *scb,
242                                unsigned int cb_break)
243 {
244         struct afs_cb_interest *old;
245         struct afs_callback *cb = &scb->callback;
246
247         if (!afs_cb_is_broken(cb_break, vnode, fc->cbi)) {
248                 vnode->cb_expires_at    = cb->expires_at;
249                 old = rcu_dereference_protected(vnode->cb_interest,
250                                                 lockdep_is_held(&vnode->cb_lock.lock));
251                 if (old != fc->cbi) {
252                         rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(fc->cbi));
253                         afs_put_cb_interest(afs_v2net(vnode), old);
254                 }
255                 set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
256         }
257 }
258
259 /*
260  * Apply the received status and callback to an inode all in the same critical
261  * section to avoid races with afs_validate().
262  */
263 void afs_vnode_commit_status(struct afs_fs_cursor *fc,
264                              struct afs_vnode *vnode,
265                              unsigned int cb_break,
266                              const afs_dataversion_t *expected_version,
267                              struct afs_status_cb *scb)
268 {
269         if (fc->ac.error != 0)
270                 return;
271
272         write_seqlock(&vnode->cb_lock);
273
274         afs_apply_status(fc, vnode, scb, expected_version);
275         if (scb->have_cb)
276                 afs_apply_callback(fc, vnode, scb, cb_break);
277
278         write_sequnlock(&vnode->cb_lock);
279
280         if (fc->ac.error == 0)
281                 afs_cache_permit(vnode, fc->key, cb_break, scb);
282 }
283
284 /*
285  * Fetch file status from the volume.
286  */
287 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
288                      afs_access_t *_caller_access)
289 {
290         struct afs_status_cb *scb;
291         struct afs_fs_cursor fc;
292         int ret;
293
294         _enter("%s,{%llx:%llu.%u,S=%lx}",
295                vnode->volume->name,
296                vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
297                vnode->flags);
298
299         scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
300         if (!scb)
301                 return -ENOMEM;
302
303         ret = -ERESTARTSYS;
304         if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
305                 afs_dataversion_t data_version = vnode->status.data_version;
306
307                 while (afs_select_fileserver(&fc)) {
308                         fc.cb_break = afs_calc_vnode_cb_break(vnode);
309                         afs_fs_fetch_file_status(&fc, scb, NULL);
310                 }
311
312                 if (fc.error) {
313                         /* Do nothing. */
314                 } else if (is_new) {
315                         ret = afs_inode_init_from_status(vnode, key, fc.cbi,
316                                                          NULL, scb);
317                         fc.error = ret;
318                         if (ret == 0)
319                                 afs_cache_permit(vnode, key, fc.cb_break, scb);
320                 } else {
321                         afs_vnode_commit_status(&fc, vnode, fc.cb_break,
322                                                 &data_version, scb);
323                 }
324                 afs_check_for_remote_deletion(&fc, vnode);
325                 ret = afs_end_vnode_operation(&fc);
326         }
327
328         if (ret == 0 && _caller_access)
329                 *_caller_access = scb->status.caller_access;
330         kfree(scb);
331         _leave(" = %d", ret);
332         return ret;
333 }
334
335 /*
336  * iget5() comparator
337  */
338 int afs_iget5_test(struct inode *inode, void *opaque)
339 {
340         struct afs_iget_data *data = opaque;
341         struct afs_vnode *vnode = AFS_FS_I(inode);
342
343         return memcmp(&vnode->fid, &data->fid, sizeof(data->fid)) == 0;
344 }
345
346 /*
347  * iget5() comparator for inode created by autocell operations
348  *
349  * These pseudo inodes don't match anything.
350  */
351 static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque)
352 {
353         return 0;
354 }
355
356 /*
357  * iget5() inode initialiser
358  */
359 static int afs_iget5_set(struct inode *inode, void *opaque)
360 {
361         struct afs_iget_data *data = opaque;
362         struct afs_vnode *vnode = AFS_FS_I(inode);
363
364         vnode->fid = data->fid;
365         vnode->volume = data->volume;
366
367         /* YFS supports 96-bit vnode IDs, but Linux only supports
368          * 64-bit inode numbers.
369          */
370         inode->i_ino = data->fid.vnode;
371         inode->i_generation = data->fid.unique;
372         return 0;
373 }
374
375 /*
376  * Create an inode for a dynamic root directory or an autocell dynamic
377  * automount dir.
378  */
379 struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
380 {
381         struct afs_iget_data data;
382         struct afs_super_info *as;
383         struct afs_vnode *vnode;
384         struct inode *inode;
385         static atomic_t afs_autocell_ino;
386
387         _enter("");
388
389         as = sb->s_fs_info;
390         if (as->volume) {
391                 data.volume = as->volume;
392                 data.fid.vid = as->volume->vid;
393         }
394         if (root) {
395                 data.fid.vnode = 1;
396                 data.fid.unique = 1;
397         } else {
398                 data.fid.vnode = atomic_inc_return(&afs_autocell_ino);
399                 data.fid.unique = 0;
400         }
401
402         inode = iget5_locked(sb, data.fid.vnode,
403                              afs_iget5_pseudo_dir_test, afs_iget5_set,
404                              &data);
405         if (!inode) {
406                 _leave(" = -ENOMEM");
407                 return ERR_PTR(-ENOMEM);
408         }
409
410         _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
411                inode, inode->i_ino, data.fid.vid, data.fid.vnode,
412                data.fid.unique);
413
414         vnode = AFS_FS_I(inode);
415
416         /* there shouldn't be an existing inode */
417         BUG_ON(!(inode->i_state & I_NEW));
418
419         inode->i_size           = 0;
420         inode->i_mode           = S_IFDIR | S_IRUGO | S_IXUGO;
421         if (root) {
422                 inode->i_op     = &afs_dynroot_inode_operations;
423                 inode->i_fop    = &afs_dynroot_file_operations;
424         } else {
425                 inode->i_op     = &afs_autocell_inode_operations;
426         }
427         set_nlink(inode, 2);
428         inode->i_uid            = GLOBAL_ROOT_UID;
429         inode->i_gid            = GLOBAL_ROOT_GID;
430         inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode);
431         inode->i_blocks         = 0;
432         inode_set_iversion_raw(inode, 0);
433         inode->i_generation     = 0;
434
435         set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
436         if (!root) {
437                 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
438                 inode->i_flags |= S_AUTOMOUNT;
439         }
440
441         inode->i_flags |= S_NOATIME;
442         unlock_new_inode(inode);
443         _leave(" = %p", inode);
444         return inode;
445 }
446
447 /*
448  * Get a cache cookie for an inode.
449  */
450 static void afs_get_inode_cache(struct afs_vnode *vnode)
451 {
452 #ifdef CONFIG_AFS_FSCACHE
453         struct {
454                 u32 vnode_id;
455                 u32 unique;
456                 u32 vnode_id_ext[2];    /* Allow for a 96-bit key */
457         } __packed key;
458         struct afs_vnode_cache_aux aux;
459
460         if (vnode->status.type == AFS_FTYPE_DIR) {
461                 vnode->cache = NULL;
462                 return;
463         }
464
465         key.vnode_id            = vnode->fid.vnode;
466         key.unique              = vnode->fid.unique;
467         key.vnode_id_ext[0]     = vnode->fid.vnode >> 32;
468         key.vnode_id_ext[1]     = vnode->fid.vnode_hi;
469         aux.data_version        = vnode->status.data_version;
470
471         vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
472                                               &afs_vnode_cache_index_def,
473                                               &key, sizeof(key),
474                                               &aux, sizeof(aux),
475                                               vnode, vnode->status.size, true);
476 #endif
477 }
478
479 /*
480  * inode retrieval
481  */
482 struct inode *afs_iget(struct super_block *sb, struct key *key,
483                        struct afs_fid *fid, struct afs_status_cb *scb,
484                        struct afs_cb_interest *cbi,
485                        struct afs_vnode *parent_vnode)
486 {
487         struct afs_iget_data data = { .fid = *fid };
488         struct afs_super_info *as;
489         struct afs_vnode *vnode;
490         struct inode *inode;
491         int ret;
492
493         _enter(",{%llx:%llu.%u},,", fid->vid, fid->vnode, fid->unique);
494
495         as = sb->s_fs_info;
496         data.volume = as->volume;
497
498         inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
499                              &data);
500         if (!inode) {
501                 _leave(" = -ENOMEM");
502                 return ERR_PTR(-ENOMEM);
503         }
504
505         _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
506                inode, fid->vid, fid->vnode, fid->unique);
507
508         vnode = AFS_FS_I(inode);
509
510         /* deal with an existing inode */
511         if (!(inode->i_state & I_NEW)) {
512                 _leave(" = %p", inode);
513                 return inode;
514         }
515
516         if (!scb) {
517                 /* it's a remotely extant inode */
518                 ret = afs_fetch_status(vnode, key, true, NULL);
519                 if (ret < 0)
520                         goto bad_inode;
521         } else {
522                 ret = afs_inode_init_from_status(vnode, key, cbi, parent_vnode,
523                                                  scb);
524                 if (ret < 0)
525                         goto bad_inode;
526         }
527
528         afs_get_inode_cache(vnode);
529
530         /* success */
531         clear_bit(AFS_VNODE_UNSET, &vnode->flags);
532         inode->i_flags |= S_NOATIME;
533         unlock_new_inode(inode);
534         _leave(" = %p", inode);
535         return inode;
536
537         /* failure */
538 bad_inode:
539         iget_failed(inode);
540         _leave(" = %d [bad]", ret);
541         return ERR_PTR(ret);
542 }
543
544 /*
545  * mark the data attached to an inode as obsolete due to a write on the server
546  * - might also want to ditch all the outstanding writes and dirty pages
547  */
548 void afs_zap_data(struct afs_vnode *vnode)
549 {
550         _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
551
552 #ifdef CONFIG_AFS_FSCACHE
553         fscache_invalidate(vnode->cache);
554 #endif
555
556         /* nuke all the non-dirty pages that aren't locked, mapped or being
557          * written back in a regular file and completely discard the pages in a
558          * directory or symlink */
559         if (S_ISREG(vnode->vfs_inode.i_mode))
560                 invalidate_remote_inode(&vnode->vfs_inode);
561         else
562                 invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
563 }
564
565 /*
566  * Check the validity of a vnode/inode.
567  */
568 bool afs_check_validity(struct afs_vnode *vnode)
569 {
570         struct afs_cb_interest *cbi;
571         struct afs_server *server;
572         struct afs_volume *volume = vnode->volume;
573         time64_t now = ktime_get_real_seconds();
574         bool valid, need_clear = false;
575         unsigned int cb_break, cb_s_break, cb_v_break;
576         int seq = 0;
577
578         do {
579                 read_seqbegin_or_lock(&vnode->cb_lock, &seq);
580                 cb_v_break = READ_ONCE(volume->cb_v_break);
581                 cb_break = vnode->cb_break;
582
583                 if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
584                         cbi = rcu_dereference(vnode->cb_interest);
585                         server = rcu_dereference(cbi->server);
586                         cb_s_break = READ_ONCE(server->cb_s_break);
587
588                         if (vnode->cb_s_break != cb_s_break ||
589                             vnode->cb_v_break != cb_v_break) {
590                                 vnode->cb_s_break = cb_s_break;
591                                 vnode->cb_v_break = cb_v_break;
592                                 need_clear = true;
593                                 valid = false;
594                         } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
595                                 need_clear = true;
596                                 valid = false;
597                         } else if (vnode->cb_expires_at - 10 <= now) {
598                                 need_clear = true;
599                                 valid = false;
600                         } else {
601                                 valid = true;
602                         }
603                 } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
604                         valid = true;
605                 } else {
606                         vnode->cb_v_break = cb_v_break;
607                         valid = false;
608                 }
609
610         } while (need_seqretry(&vnode->cb_lock, seq));
611
612         done_seqretry(&vnode->cb_lock, seq);
613
614         if (need_clear) {
615                 write_seqlock(&vnode->cb_lock);
616                 if (cb_break == vnode->cb_break)
617                         __afs_break_callback(vnode);
618                 write_sequnlock(&vnode->cb_lock);
619                 valid = false;
620         }
621
622         return valid;
623 }
624
625 /*
626  * validate a vnode/inode
627  * - there are several things we need to check
628  *   - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
629  *     symlink)
630  *   - parent dir metadata changed (security changes)
631  *   - dentry data changed (write, truncate)
632  *   - dentry metadata changed (security changes)
633  */
634 int afs_validate(struct afs_vnode *vnode, struct key *key)
635 {
636         bool valid;
637         int ret;
638
639         _enter("{v={%llx:%llu} fl=%lx},%x",
640                vnode->fid.vid, vnode->fid.vnode, vnode->flags,
641                key_serial(key));
642
643         rcu_read_lock();
644         valid = afs_check_validity(vnode);
645         rcu_read_unlock();
646
647         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
648                 clear_nlink(&vnode->vfs_inode);
649
650         if (valid)
651                 goto valid;
652
653         down_write(&vnode->validate_lock);
654
655         /* if the promise has expired, we need to check the server again to get
656          * a new promise - note that if the (parent) directory's metadata was
657          * changed then the security may be different and we may no longer have
658          * access */
659         if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
660                 _debug("not promised");
661                 ret = afs_fetch_status(vnode, key, false, NULL);
662                 if (ret < 0) {
663                         if (ret == -ENOENT) {
664                                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
665                                 ret = -ESTALE;
666                         }
667                         goto error_unlock;
668                 }
669                 _debug("new promise [fl=%lx]", vnode->flags);
670         }
671
672         if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
673                 _debug("file already deleted");
674                 ret = -ESTALE;
675                 goto error_unlock;
676         }
677
678         /* if the vnode's data version number changed then its contents are
679          * different */
680         if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
681                 afs_zap_data(vnode);
682         up_write(&vnode->validate_lock);
683 valid:
684         _leave(" = 0");
685         return 0;
686
687 error_unlock:
688         up_write(&vnode->validate_lock);
689         _leave(" = %d", ret);
690         return ret;
691 }
692
693 /*
694  * read the attributes of an inode
695  */
696 int afs_getattr(const struct path *path, struct kstat *stat,
697                 u32 request_mask, unsigned int query_flags)
698 {
699         struct inode *inode = d_inode(path->dentry);
700         struct afs_vnode *vnode = AFS_FS_I(inode);
701         int seq = 0;
702
703         _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
704
705         do {
706                 read_seqbegin_or_lock(&vnode->cb_lock, &seq);
707                 generic_fillattr(inode, stat);
708         } while (need_seqretry(&vnode->cb_lock, seq));
709
710         done_seqretry(&vnode->cb_lock, seq);
711         return 0;
712 }
713
714 /*
715  * discard an AFS inode
716  */
717 int afs_drop_inode(struct inode *inode)
718 {
719         _enter("");
720
721         if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
722                 return generic_delete_inode(inode);
723         else
724                 return generic_drop_inode(inode);
725 }
726
727 /*
728  * clear an AFS inode
729  */
730 void afs_evict_inode(struct inode *inode)
731 {
732         struct afs_cb_interest *cbi;
733         struct afs_vnode *vnode;
734
735         vnode = AFS_FS_I(inode);
736
737         _enter("{%llx:%llu.%d}",
738                vnode->fid.vid,
739                vnode->fid.vnode,
740                vnode->fid.unique);
741
742         _debug("CLEAR INODE %p", inode);
743
744         ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
745
746         truncate_inode_pages_final(&inode->i_data);
747         clear_inode(inode);
748
749         write_seqlock(&vnode->cb_lock);
750         cbi = rcu_dereference_protected(vnode->cb_interest,
751                                         lockdep_is_held(&vnode->cb_lock.lock));
752         if (cbi) {
753                 afs_put_cb_interest(afs_i2net(inode), cbi);
754                 rcu_assign_pointer(vnode->cb_interest, NULL);
755         }
756         write_sequnlock(&vnode->cb_lock);
757
758         while (!list_empty(&vnode->wb_keys)) {
759                 struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
760                                                     struct afs_wb_key, vnode_link);
761                 list_del(&wbk->vnode_link);
762                 afs_put_wb_key(wbk);
763         }
764
765 #ifdef CONFIG_AFS_FSCACHE
766         {
767                 struct afs_vnode_cache_aux aux;
768
769                 aux.data_version = vnode->status.data_version;
770                 fscache_relinquish_cookie(vnode->cache, &aux,
771                                           test_bit(AFS_VNODE_DELETED, &vnode->flags));
772                 vnode->cache = NULL;
773         }
774 #endif
775
776         afs_prune_wb_keys(vnode);
777         afs_put_permits(rcu_access_pointer(vnode->permit_cache));
778         key_put(vnode->silly_key);
779         vnode->silly_key = NULL;
780         key_put(vnode->lock_key);
781         vnode->lock_key = NULL;
782         _leave("");
783 }
784
785 /*
786  * set the attributes of an inode
787  */
788 int afs_setattr(struct dentry *dentry, struct iattr *attr)
789 {
790         struct afs_fs_cursor fc;
791         struct afs_status_cb *scb;
792         struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
793         struct key *key;
794         int ret = -ENOMEM;
795
796         _enter("{%llx:%llu},{n=%pd},%x",
797                vnode->fid.vid, vnode->fid.vnode, dentry,
798                attr->ia_valid);
799
800         if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
801                                 ATTR_MTIME))) {
802                 _leave(" = 0 [unsupported]");
803                 return 0;
804         }
805
806         scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
807         if (!scb)
808                 goto error;
809
810         /* flush any dirty data outstanding on a regular file */
811         if (S_ISREG(vnode->vfs_inode.i_mode))
812                 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
813
814         if (attr->ia_valid & ATTR_FILE) {
815                 key = afs_file_key(attr->ia_file);
816         } else {
817                 key = afs_request_key(vnode->volume->cell);
818                 if (IS_ERR(key)) {
819                         ret = PTR_ERR(key);
820                         goto error_scb;
821                 }
822         }
823
824         ret = -ERESTARTSYS;
825         if (afs_begin_vnode_operation(&fc, vnode, key, false)) {
826                 afs_dataversion_t data_version = vnode->status.data_version;
827
828                 if (attr->ia_valid & ATTR_SIZE)
829                         data_version++;
830
831                 while (afs_select_fileserver(&fc)) {
832                         fc.cb_break = afs_calc_vnode_cb_break(vnode);
833                         afs_fs_setattr(&fc, attr, scb);
834                 }
835
836                 afs_check_for_remote_deletion(&fc, vnode);
837                 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
838                                         &data_version, scb);
839                 ret = afs_end_vnode_operation(&fc);
840         }
841
842         if (!(attr->ia_valid & ATTR_FILE))
843                 key_put(key);
844
845 error_scb:
846         kfree(scb);
847 error:
848         _leave(" = %d", ret);
849         return ret;
850 }