Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
0003  *
0004  * This software may be freely redistributed under the terms of the
0005  * GNU General Public License.
0006  *
0007  * You should have received a copy of the GNU General Public License
0008  * along with this program; if not, write to the Free Software
0009  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0010  *
0011  * Authors: David Woodhouse <dwmw2@infradead.org>
0012  *          David Howells <dhowells@redhat.com>
0013  *
0014  */
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/init.h>
0019 #include <linux/fs.h>
0020 #include <linux/pagemap.h>
0021 #include <linux/sched.h>
0022 #include <linux/mount.h>
0023 #include <linux/namei.h>
0024 #include <linux/iversion.h>
0025 #include "internal.h"
0026 #include "afs_fs.h"
0027 
0028 static const struct inode_operations afs_symlink_inode_operations = {
0029     .get_link   = page_get_link,
0030 };
0031 
0032 static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
0033 {
0034     static unsigned long once_only;
0035 
0036     pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode->status.type);
0037     pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
0038         vnode->status.abort_code,
0039         vnode->status.mode,
0040         vnode->status.size,
0041         vnode->status.data_version);
0042     pr_warn("kAFS: vnode %llx:%llx:%x\n",
0043         vnode->fid.vid,
0044         vnode->fid.vnode,
0045         vnode->fid.unique);
0046     if (parent_vnode)
0047         pr_warn("kAFS: dir %llx:%llx:%x\n",
0048             parent_vnode->fid.vid,
0049             parent_vnode->fid.vnode,
0050             parent_vnode->fid.unique);
0051 
0052     if (!test_and_set_bit(0, &once_only))
0053         dump_stack();
0054 }
0055 
0056 /*
0057  * Set parameters for the netfs library
0058  */
0059 static void afs_set_netfs_context(struct afs_vnode *vnode)
0060 {
0061     netfs_inode_init(&vnode->netfs, &afs_req_ops);
0062 }
0063 
0064 /*
0065  * Initialise an inode from the vnode status.
0066  */
0067 static int afs_inode_init_from_status(struct afs_operation *op,
0068                       struct afs_vnode_param *vp,
0069                       struct afs_vnode *vnode)
0070 {
0071     struct afs_file_status *status = &vp->scb.status;
0072     struct inode *inode = AFS_VNODE_TO_I(vnode);
0073     struct timespec64 t;
0074 
0075     _enter("{%llx:%llu.%u} %s",
0076            vp->fid.vid, vp->fid.vnode, vp->fid.unique,
0077            op->type ? op->type->name : "???");
0078 
0079     _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
0080            status->type,
0081            status->nlink,
0082            (unsigned long long) status->size,
0083            status->data_version,
0084            status->mode);
0085 
0086     write_seqlock(&vnode->cb_lock);
0087 
0088     vnode->cb_v_break = op->cb_v_break;
0089     vnode->cb_s_break = op->cb_s_break;
0090     vnode->status = *status;
0091 
0092     t = status->mtime_client;
0093     inode->i_ctime = t;
0094     inode->i_mtime = t;
0095     inode->i_atime = t;
0096     inode->i_flags |= S_NOATIME;
0097     inode->i_uid = make_kuid(&init_user_ns, status->owner);
0098     inode->i_gid = make_kgid(&init_user_ns, status->group);
0099     set_nlink(&vnode->netfs.inode, status->nlink);
0100 
0101     switch (status->type) {
0102     case AFS_FTYPE_FILE:
0103         inode->i_mode   = S_IFREG | (status->mode & S_IALLUGO);
0104         inode->i_op = &afs_file_inode_operations;
0105         inode->i_fop    = &afs_file_operations;
0106         inode->i_mapping->a_ops = &afs_file_aops;
0107         mapping_set_large_folios(inode->i_mapping);
0108         break;
0109     case AFS_FTYPE_DIR:
0110         inode->i_mode   = S_IFDIR |  (status->mode & S_IALLUGO);
0111         inode->i_op = &afs_dir_inode_operations;
0112         inode->i_fop    = &afs_dir_file_operations;
0113         inode->i_mapping->a_ops = &afs_dir_aops;
0114         mapping_set_large_folios(inode->i_mapping);
0115         break;
0116     case AFS_FTYPE_SYMLINK:
0117         /* Symlinks with a mode of 0644 are actually mountpoints. */
0118         if ((status->mode & 0777) == 0644) {
0119             inode->i_flags |= S_AUTOMOUNT;
0120 
0121             set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
0122 
0123             inode->i_mode   = S_IFDIR | 0555;
0124             inode->i_op = &afs_mntpt_inode_operations;
0125             inode->i_fop    = &afs_mntpt_file_operations;
0126             inode->i_mapping->a_ops = &afs_symlink_aops;
0127         } else {
0128             inode->i_mode   = S_IFLNK | status->mode;
0129             inode->i_op = &afs_symlink_inode_operations;
0130             inode->i_mapping->a_ops = &afs_symlink_aops;
0131         }
0132         inode_nohighmem(inode);
0133         break;
0134     default:
0135         dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL);
0136         write_sequnlock(&vnode->cb_lock);
0137         return afs_protocol_error(NULL, afs_eproto_file_type);
0138     }
0139 
0140     afs_set_i_size(vnode, status->size);
0141     afs_set_netfs_context(vnode);
0142 
0143     vnode->invalid_before   = status->data_version;
0144     inode_set_iversion_raw(&vnode->netfs.inode, status->data_version);
0145 
0146     if (!vp->scb.have_cb) {
0147         /* it's a symlink we just created (the fileserver
0148          * didn't give us a callback) */
0149         vnode->cb_expires_at = ktime_get_real_seconds();
0150     } else {
0151         vnode->cb_expires_at = vp->scb.callback.expires_at;
0152         vnode->cb_server = op->server;
0153         set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
0154     }
0155 
0156     write_sequnlock(&vnode->cb_lock);
0157     return 0;
0158 }
0159 
0160 /*
0161  * Update the core inode struct from a returned status record.
0162  */
0163 static void afs_apply_status(struct afs_operation *op,
0164                  struct afs_vnode_param *vp)
0165 {
0166     struct afs_file_status *status = &vp->scb.status;
0167     struct afs_vnode *vnode = vp->vnode;
0168     struct inode *inode = &vnode->netfs.inode;
0169     struct timespec64 t;
0170     umode_t mode;
0171     bool data_changed = false;
0172     bool change_size = vp->set_size;
0173 
0174     _enter("{%llx:%llu.%u} %s",
0175            vp->fid.vid, vp->fid.vnode, vp->fid.unique,
0176            op->type ? op->type->name : "???");
0177 
0178     BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
0179 
0180     if (status->type != vnode->status.type) {
0181         pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
0182             vnode->fid.vid,
0183             vnode->fid.vnode,
0184             vnode->fid.unique,
0185             status->type, vnode->status.type);
0186         afs_protocol_error(NULL, afs_eproto_bad_status);
0187         return;
0188     }
0189 
0190     if (status->nlink != vnode->status.nlink)
0191         set_nlink(inode, status->nlink);
0192 
0193     if (status->owner != vnode->status.owner)
0194         inode->i_uid = make_kuid(&init_user_ns, status->owner);
0195 
0196     if (status->group != vnode->status.group)
0197         inode->i_gid = make_kgid(&init_user_ns, status->group);
0198 
0199     if (status->mode != vnode->status.mode) {
0200         mode = inode->i_mode;
0201         mode &= ~S_IALLUGO;
0202         mode |= status->mode & S_IALLUGO;
0203         WRITE_ONCE(inode->i_mode, mode);
0204     }
0205 
0206     t = status->mtime_client;
0207     inode->i_mtime = t;
0208     if (vp->update_ctime)
0209         inode->i_ctime = op->ctime;
0210 
0211     if (vnode->status.data_version != status->data_version)
0212         data_changed = true;
0213 
0214     vnode->status = *status;
0215 
0216     if (vp->dv_before + vp->dv_delta != status->data_version) {
0217         if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
0218             pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
0219                 vnode->fid.vid, vnode->fid.vnode,
0220                 (unsigned long long)vp->dv_before + vp->dv_delta,
0221                 (unsigned long long)status->data_version,
0222                 op->type ? op->type->name : "???",
0223                 op->debug_id);
0224 
0225         vnode->invalid_before = status->data_version;
0226         if (vnode->status.type == AFS_FTYPE_DIR) {
0227             if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
0228                 afs_stat_v(vnode, n_inval);
0229         } else {
0230             set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
0231         }
0232         change_size = true;
0233     } else if (vnode->status.type == AFS_FTYPE_DIR) {
0234         /* Expected directory change is handled elsewhere so
0235          * that we can locally edit the directory and save on a
0236          * download.
0237          */
0238         if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
0239             data_changed = false;
0240         change_size = true;
0241     }
0242 
0243     if (data_changed) {
0244         inode_set_iversion_raw(inode, status->data_version);
0245 
0246         /* Only update the size if the data version jumped.  If the
0247          * file is being modified locally, then we might have our own
0248          * idea of what the size should be that's not the same as
0249          * what's on the server.
0250          */
0251         vnode->netfs.remote_i_size = status->size;
0252         if (change_size) {
0253             afs_set_i_size(vnode, status->size);
0254             inode->i_ctime = t;
0255             inode->i_atime = t;
0256         }
0257     }
0258 }
0259 
0260 /*
0261  * Apply a callback to a vnode.
0262  */
0263 static void afs_apply_callback(struct afs_operation *op,
0264                    struct afs_vnode_param *vp)
0265 {
0266     struct afs_callback *cb = &vp->scb.callback;
0267     struct afs_vnode *vnode = vp->vnode;
0268 
0269     if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
0270         vnode->cb_expires_at    = cb->expires_at;
0271         vnode->cb_server    = op->server;
0272         set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
0273     }
0274 }
0275 
0276 /*
0277  * Apply the received status and callback to an inode all in the same critical
0278  * section to avoid races with afs_validate().
0279  */
0280 void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp)
0281 {
0282     struct afs_vnode *vnode = vp->vnode;
0283 
0284     _enter("");
0285 
0286     write_seqlock(&vnode->cb_lock);
0287 
0288     if (vp->scb.have_error) {
0289         /* A YFS server will return this from RemoveFile2 and AFS and
0290          * YFS will return this from InlineBulkStatus.
0291          */
0292         if (vp->scb.status.abort_code == VNOVNODE) {
0293             set_bit(AFS_VNODE_DELETED, &vnode->flags);
0294             clear_nlink(&vnode->netfs.inode);
0295             __afs_break_callback(vnode, afs_cb_break_for_deleted);
0296             op->flags &= ~AFS_OPERATION_DIR_CONFLICT;
0297         }
0298     } else if (vp->scb.have_status) {
0299         if (vp->speculative &&
0300             (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) ||
0301              vp->dv_before != vnode->status.data_version))
0302             /* Ignore the result of a speculative bulk status fetch
0303              * if it splits around a modification op, thereby
0304              * appearing to regress the data version.
0305              */
0306             goto out;
0307         afs_apply_status(op, vp);
0308         if (vp->scb.have_cb)
0309             afs_apply_callback(op, vp);
0310     } else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) {
0311         drop_nlink(&vnode->netfs.inode);
0312         if (vnode->netfs.inode.i_nlink == 0) {
0313             set_bit(AFS_VNODE_DELETED, &vnode->flags);
0314             __afs_break_callback(vnode, afs_cb_break_for_deleted);
0315         }
0316     }
0317 
0318 out:
0319     write_sequnlock(&vnode->cb_lock);
0320 
0321     if (vp->scb.have_status)
0322         afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
0323 }
0324 
0325 static void afs_fetch_status_success(struct afs_operation *op)
0326 {
0327     struct afs_vnode_param *vp = &op->file[op->fetch_status.which];
0328     struct afs_vnode *vnode = vp->vnode;
0329     int ret;
0330 
0331     if (vnode->netfs.inode.i_state & I_NEW) {
0332         ret = afs_inode_init_from_status(op, vp, vnode);
0333         op->error = ret;
0334         if (ret == 0)
0335             afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
0336     } else {
0337         afs_vnode_commit_status(op, vp);
0338     }
0339 }
0340 
0341 const struct afs_operation_ops afs_fetch_status_operation = {
0342     .issue_afs_rpc  = afs_fs_fetch_status,
0343     .issue_yfs_rpc  = yfs_fs_fetch_status,
0344     .success    = afs_fetch_status_success,
0345     .aborted    = afs_check_for_remote_deletion,
0346 };
0347 
0348 /*
0349  * Fetch file status from the volume.
0350  */
0351 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
0352              afs_access_t *_caller_access)
0353 {
0354     struct afs_operation *op;
0355 
0356     _enter("%s,{%llx:%llu.%u,S=%lx}",
0357            vnode->volume->name,
0358            vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
0359            vnode->flags);
0360 
0361     op = afs_alloc_operation(key, vnode->volume);
0362     if (IS_ERR(op))
0363         return PTR_ERR(op);
0364 
0365     afs_op_set_vnode(op, 0, vnode);
0366 
0367     op->nr_files    = 1;
0368     op->ops     = &afs_fetch_status_operation;
0369     afs_begin_vnode_operation(op);
0370     afs_wait_for_operation(op);
0371 
0372     if (_caller_access)
0373         *_caller_access = op->file[0].scb.status.caller_access;
0374     return afs_put_operation(op);
0375 }
0376 
0377 /*
0378  * ilookup() comparator
0379  */
0380 int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque)
0381 {
0382     struct afs_vnode *vnode = AFS_FS_I(inode);
0383     struct afs_fid *fid = opaque;
0384 
0385     return (fid->vnode == vnode->fid.vnode &&
0386         fid->vnode_hi == vnode->fid.vnode_hi &&
0387         fid->unique == vnode->fid.unique);
0388 }
0389 
0390 /*
0391  * iget5() comparator
0392  */
0393 static int afs_iget5_test(struct inode *inode, void *opaque)
0394 {
0395     struct afs_vnode_param *vp = opaque;
0396     //struct afs_vnode *vnode = AFS_FS_I(inode);
0397 
0398     return afs_ilookup5_test_by_fid(inode, &vp->fid);
0399 }
0400 
0401 /*
0402  * iget5() inode initialiser
0403  */
0404 static int afs_iget5_set(struct inode *inode, void *opaque)
0405 {
0406     struct afs_vnode_param *vp = opaque;
0407     struct afs_super_info *as = AFS_FS_S(inode->i_sb);
0408     struct afs_vnode *vnode = AFS_FS_I(inode);
0409 
0410     vnode->volume       = as->volume;
0411     vnode->fid      = vp->fid;
0412 
0413     /* YFS supports 96-bit vnode IDs, but Linux only supports
0414      * 64-bit inode numbers.
0415      */
0416     inode->i_ino        = vnode->fid.vnode;
0417     inode->i_generation = vnode->fid.unique;
0418     return 0;
0419 }
0420 
0421 /*
0422  * Get a cache cookie for an inode.
0423  */
0424 static void afs_get_inode_cache(struct afs_vnode *vnode)
0425 {
0426 #ifdef CONFIG_AFS_FSCACHE
0427     struct {
0428         __be32 vnode_id;
0429         __be32 unique;
0430         __be32 vnode_id_ext[2]; /* Allow for a 96-bit key */
0431     } __packed key;
0432     struct afs_vnode_cache_aux aux;
0433 
0434     if (vnode->status.type != AFS_FTYPE_FILE) {
0435         vnode->netfs.cache = NULL;
0436         return;
0437     }
0438 
0439     key.vnode_id        = htonl(vnode->fid.vnode);
0440     key.unique      = htonl(vnode->fid.unique);
0441     key.vnode_id_ext[0] = htonl(vnode->fid.vnode >> 32);
0442     key.vnode_id_ext[1] = htonl(vnode->fid.vnode_hi);
0443     afs_set_cache_aux(vnode, &aux);
0444 
0445     afs_vnode_set_cache(vnode,
0446                 fscache_acquire_cookie(
0447                     vnode->volume->cache,
0448                     vnode->status.type == AFS_FTYPE_FILE ?
0449                     0 : FSCACHE_ADV_SINGLE_CHUNK,
0450                     &key, sizeof(key),
0451                     &aux, sizeof(aux),
0452                     vnode->status.size));
0453 #endif
0454 }
0455 
0456 /*
0457  * inode retrieval
0458  */
0459 struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
0460 {
0461     struct afs_vnode_param *dvp = &op->file[0];
0462     struct super_block *sb = dvp->vnode->netfs.inode.i_sb;
0463     struct afs_vnode *vnode;
0464     struct inode *inode;
0465     int ret;
0466 
0467     _enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
0468 
0469     inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
0470     if (!inode) {
0471         _leave(" = -ENOMEM");
0472         return ERR_PTR(-ENOMEM);
0473     }
0474 
0475     vnode = AFS_FS_I(inode);
0476 
0477     _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
0478            inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
0479 
0480     /* deal with an existing inode */
0481     if (!(inode->i_state & I_NEW)) {
0482         _leave(" = %p", inode);
0483         return inode;
0484     }
0485 
0486     ret = afs_inode_init_from_status(op, vp, vnode);
0487     if (ret < 0)
0488         goto bad_inode;
0489 
0490     afs_get_inode_cache(vnode);
0491 
0492     /* success */
0493     clear_bit(AFS_VNODE_UNSET, &vnode->flags);
0494     unlock_new_inode(inode);
0495     _leave(" = %p", inode);
0496     return inode;
0497 
0498     /* failure */
0499 bad_inode:
0500     iget_failed(inode);
0501     _leave(" = %d [bad]", ret);
0502     return ERR_PTR(ret);
0503 }
0504 
0505 static int afs_iget5_set_root(struct inode *inode, void *opaque)
0506 {
0507     struct afs_super_info *as = AFS_FS_S(inode->i_sb);
0508     struct afs_vnode *vnode = AFS_FS_I(inode);
0509 
0510     vnode->volume       = as->volume;
0511     vnode->fid.vid      = as->volume->vid,
0512     vnode->fid.vnode    = 1;
0513     vnode->fid.unique   = 1;
0514     inode->i_ino        = 1;
0515     inode->i_generation = 1;
0516     return 0;
0517 }
0518 
0519 /*
0520  * Set up the root inode for a volume.  This is always vnode 1, unique 1 within
0521  * the volume.
0522  */
0523 struct inode *afs_root_iget(struct super_block *sb, struct key *key)
0524 {
0525     struct afs_super_info *as = AFS_FS_S(sb);
0526     struct afs_operation *op;
0527     struct afs_vnode *vnode;
0528     struct inode *inode;
0529     int ret;
0530 
0531     _enter(",{%llx},,", as->volume->vid);
0532 
0533     inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
0534     if (!inode) {
0535         _leave(" = -ENOMEM");
0536         return ERR_PTR(-ENOMEM);
0537     }
0538 
0539     _debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
0540 
0541     BUG_ON(!(inode->i_state & I_NEW));
0542 
0543     vnode = AFS_FS_I(inode);
0544     vnode->cb_v_break = as->volume->cb_v_break,
0545     afs_set_netfs_context(vnode);
0546 
0547     op = afs_alloc_operation(key, as->volume);
0548     if (IS_ERR(op)) {
0549         ret = PTR_ERR(op);
0550         goto error;
0551     }
0552 
0553     afs_op_set_vnode(op, 0, vnode);
0554 
0555     op->nr_files    = 1;
0556     op->ops     = &afs_fetch_status_operation;
0557     ret = afs_do_sync_operation(op);
0558     if (ret < 0)
0559         goto error;
0560 
0561     afs_get_inode_cache(vnode);
0562 
0563     clear_bit(AFS_VNODE_UNSET, &vnode->flags);
0564     unlock_new_inode(inode);
0565     _leave(" = %p", inode);
0566     return inode;
0567 
0568 error:
0569     iget_failed(inode);
0570     _leave(" = %d [bad]", ret);
0571     return ERR_PTR(ret);
0572 }
0573 
0574 /*
0575  * mark the data attached to an inode as obsolete due to a write on the server
0576  * - might also want to ditch all the outstanding writes and dirty pages
0577  */
0578 static void afs_zap_data(struct afs_vnode *vnode)
0579 {
0580     _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
0581 
0582     afs_invalidate_cache(vnode, 0);
0583 
0584     /* nuke all the non-dirty pages that aren't locked, mapped or being
0585      * written back in a regular file and completely discard the pages in a
0586      * directory or symlink */
0587     if (S_ISREG(vnode->netfs.inode.i_mode))
0588         invalidate_remote_inode(&vnode->netfs.inode);
0589     else
0590         invalidate_inode_pages2(vnode->netfs.inode.i_mapping);
0591 }
0592 
0593 /*
0594  * Check to see if we have a server currently serving this volume and that it
0595  * hasn't been reinitialised or dropped from the list.
0596  */
0597 static bool afs_check_server_good(struct afs_vnode *vnode)
0598 {
0599     struct afs_server_list *slist;
0600     struct afs_server *server;
0601     bool good;
0602     int i;
0603 
0604     if (vnode->cb_fs_s_break == atomic_read(&vnode->volume->cell->fs_s_break))
0605         return true;
0606 
0607     rcu_read_lock();
0608 
0609     slist = rcu_dereference(vnode->volume->servers);
0610     for (i = 0; i < slist->nr_servers; i++) {
0611         server = slist->servers[i].server;
0612         if (server == vnode->cb_server) {
0613             good = (vnode->cb_s_break == server->cb_s_break);
0614             rcu_read_unlock();
0615             return good;
0616         }
0617     }
0618 
0619     rcu_read_unlock();
0620     return false;
0621 }
0622 
0623 /*
0624  * Check the validity of a vnode/inode.
0625  */
0626 bool afs_check_validity(struct afs_vnode *vnode)
0627 {
0628     enum afs_cb_break_reason need_clear = afs_cb_break_no_break;
0629     time64_t now = ktime_get_real_seconds();
0630     unsigned int cb_break;
0631     int seq = 0;
0632 
0633     do {
0634         read_seqbegin_or_lock(&vnode->cb_lock, &seq);
0635         cb_break = vnode->cb_break;
0636 
0637         if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
0638             if (vnode->cb_v_break != vnode->volume->cb_v_break)
0639                 need_clear = afs_cb_break_for_v_break;
0640             else if (!afs_check_server_good(vnode))
0641                 need_clear = afs_cb_break_for_s_reinit;
0642             else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
0643                 need_clear = afs_cb_break_for_zap;
0644             else if (vnode->cb_expires_at - 10 <= now)
0645                 need_clear = afs_cb_break_for_lapsed;
0646         } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
0647             ;
0648         } else {
0649             need_clear = afs_cb_break_no_promise;
0650         }
0651 
0652     } while (need_seqretry(&vnode->cb_lock, seq));
0653 
0654     done_seqretry(&vnode->cb_lock, seq);
0655 
0656     if (need_clear == afs_cb_break_no_break)
0657         return true;
0658 
0659     write_seqlock(&vnode->cb_lock);
0660     if (need_clear == afs_cb_break_no_promise)
0661         vnode->cb_v_break = vnode->volume->cb_v_break;
0662     else if (cb_break == vnode->cb_break)
0663         __afs_break_callback(vnode, need_clear);
0664     else
0665         trace_afs_cb_miss(&vnode->fid, need_clear);
0666     write_sequnlock(&vnode->cb_lock);
0667     return false;
0668 }
0669 
0670 /*
0671  * validate a vnode/inode
0672  * - there are several things we need to check
0673  *   - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
0674  *     symlink)
0675  *   - parent dir metadata changed (security changes)
0676  *   - dentry data changed (write, truncate)
0677  *   - dentry metadata changed (security changes)
0678  */
0679 int afs_validate(struct afs_vnode *vnode, struct key *key)
0680 {
0681     int ret;
0682 
0683     _enter("{v={%llx:%llu} fl=%lx},%x",
0684            vnode->fid.vid, vnode->fid.vnode, vnode->flags,
0685            key_serial(key));
0686 
0687     if (unlikely(test_bit(AFS_VNODE_DELETED, &vnode->flags))) {
0688         if (vnode->netfs.inode.i_nlink)
0689             clear_nlink(&vnode->netfs.inode);
0690         goto valid;
0691     }
0692 
0693     if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags) &&
0694         afs_check_validity(vnode))
0695         goto valid;
0696 
0697     down_write(&vnode->validate_lock);
0698 
0699     /* if the promise has expired, we need to check the server again to get
0700      * a new promise - note that if the (parent) directory's metadata was
0701      * changed then the security may be different and we may no longer have
0702      * access */
0703     if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
0704         _debug("not promised");
0705         ret = afs_fetch_status(vnode, key, false, NULL);
0706         if (ret < 0) {
0707             if (ret == -ENOENT) {
0708                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
0709                 ret = -ESTALE;
0710             }
0711             goto error_unlock;
0712         }
0713         _debug("new promise [fl=%lx]", vnode->flags);
0714     }
0715 
0716     if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
0717         _debug("file already deleted");
0718         ret = -ESTALE;
0719         goto error_unlock;
0720     }
0721 
0722     /* if the vnode's data version number changed then its contents are
0723      * different */
0724     if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
0725         afs_zap_data(vnode);
0726     up_write(&vnode->validate_lock);
0727 valid:
0728     _leave(" = 0");
0729     return 0;
0730 
0731 error_unlock:
0732     up_write(&vnode->validate_lock);
0733     _leave(" = %d", ret);
0734     return ret;
0735 }
0736 
0737 /*
0738  * read the attributes of an inode
0739  */
0740 int afs_getattr(struct user_namespace *mnt_userns, const struct path *path,
0741         struct kstat *stat, u32 request_mask, unsigned int query_flags)
0742 {
0743     struct inode *inode = d_inode(path->dentry);
0744     struct afs_vnode *vnode = AFS_FS_I(inode);
0745     struct key *key;
0746     int ret, seq = 0;
0747 
0748     _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
0749 
0750     if (vnode->volume &&
0751         !(query_flags & AT_STATX_DONT_SYNC) &&
0752         !test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
0753         key = afs_request_key(vnode->volume->cell);
0754         if (IS_ERR(key))
0755             return PTR_ERR(key);
0756         ret = afs_validate(vnode, key);
0757         key_put(key);
0758         if (ret < 0)
0759             return ret;
0760     }
0761 
0762     do {
0763         read_seqbegin_or_lock(&vnode->cb_lock, &seq);
0764         generic_fillattr(&init_user_ns, inode, stat);
0765         if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) &&
0766             stat->nlink > 0)
0767             stat->nlink -= 1;
0768     } while (need_seqretry(&vnode->cb_lock, seq));
0769 
0770     done_seqretry(&vnode->cb_lock, seq);
0771     return 0;
0772 }
0773 
0774 /*
0775  * discard an AFS inode
0776  */
0777 int afs_drop_inode(struct inode *inode)
0778 {
0779     _enter("");
0780 
0781     if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
0782         return generic_delete_inode(inode);
0783     else
0784         return generic_drop_inode(inode);
0785 }
0786 
0787 /*
0788  * clear an AFS inode
0789  */
0790 void afs_evict_inode(struct inode *inode)
0791 {
0792     struct afs_vnode_cache_aux aux;
0793     struct afs_vnode *vnode = AFS_FS_I(inode);
0794 
0795     _enter("{%llx:%llu.%d}",
0796            vnode->fid.vid,
0797            vnode->fid.vnode,
0798            vnode->fid.unique);
0799 
0800     _debug("CLEAR INODE %p", inode);
0801 
0802     ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
0803 
0804     truncate_inode_pages_final(&inode->i_data);
0805 
0806     afs_set_cache_aux(vnode, &aux);
0807     fscache_clear_inode_writeback(afs_vnode_cache(vnode), inode, &aux);
0808     clear_inode(inode);
0809 
0810     while (!list_empty(&vnode->wb_keys)) {
0811         struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
0812                             struct afs_wb_key, vnode_link);
0813         list_del(&wbk->vnode_link);
0814         afs_put_wb_key(wbk);
0815     }
0816 
0817     fscache_relinquish_cookie(afs_vnode_cache(vnode),
0818                   test_bit(AFS_VNODE_DELETED, &vnode->flags));
0819 
0820     afs_prune_wb_keys(vnode);
0821     afs_put_permits(rcu_access_pointer(vnode->permit_cache));
0822     key_put(vnode->silly_key);
0823     vnode->silly_key = NULL;
0824     key_put(vnode->lock_key);
0825     vnode->lock_key = NULL;
0826     _leave("");
0827 }
0828 
0829 static void afs_setattr_success(struct afs_operation *op)
0830 {
0831     struct afs_vnode_param *vp = &op->file[0];
0832     struct inode *inode = &vp->vnode->netfs.inode;
0833     loff_t old_i_size = i_size_read(inode);
0834 
0835     op->setattr.old_i_size = old_i_size;
0836     afs_vnode_commit_status(op, vp);
0837     /* inode->i_size has now been changed. */
0838 
0839     if (op->setattr.attr->ia_valid & ATTR_SIZE) {
0840         loff_t size = op->setattr.attr->ia_size;
0841         if (size > old_i_size)
0842             pagecache_isize_extended(inode, old_i_size, size);
0843     }
0844 }
0845 
0846 static void afs_setattr_edit_file(struct afs_operation *op)
0847 {
0848     struct afs_vnode_param *vp = &op->file[0];
0849     struct inode *inode = &vp->vnode->netfs.inode;
0850 
0851     if (op->setattr.attr->ia_valid & ATTR_SIZE) {
0852         loff_t size = op->setattr.attr->ia_size;
0853         loff_t i_size = op->setattr.old_i_size;
0854 
0855         if (size < i_size)
0856             truncate_pagecache(inode, size);
0857         if (size != i_size)
0858             fscache_resize_cookie(afs_vnode_cache(vp->vnode),
0859                           vp->scb.status.size);
0860     }
0861 }
0862 
0863 static const struct afs_operation_ops afs_setattr_operation = {
0864     .issue_afs_rpc  = afs_fs_setattr,
0865     .issue_yfs_rpc  = yfs_fs_setattr,
0866     .success    = afs_setattr_success,
0867     .edit_dir   = afs_setattr_edit_file,
0868 };
0869 
0870 /*
0871  * set the attributes of an inode
0872  */
0873 int afs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0874         struct iattr *attr)
0875 {
0876     const unsigned int supported =
0877         ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
0878         ATTR_MTIME | ATTR_MTIME_SET | ATTR_TIMES_SET | ATTR_TOUCH;
0879     struct afs_operation *op;
0880     struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
0881     struct inode *inode = &vnode->netfs.inode;
0882     loff_t i_size;
0883     int ret;
0884 
0885     _enter("{%llx:%llu},{n=%pd},%x",
0886            vnode->fid.vid, vnode->fid.vnode, dentry,
0887            attr->ia_valid);
0888 
0889     if (!(attr->ia_valid & supported)) {
0890         _leave(" = 0 [unsupported]");
0891         return 0;
0892     }
0893 
0894     i_size = i_size_read(inode);
0895     if (attr->ia_valid & ATTR_SIZE) {
0896         if (!S_ISREG(inode->i_mode))
0897             return -EISDIR;
0898 
0899         ret = inode_newsize_ok(inode, attr->ia_size);
0900         if (ret)
0901             return ret;
0902 
0903         if (attr->ia_size == i_size)
0904             attr->ia_valid &= ~ATTR_SIZE;
0905     }
0906 
0907     fscache_use_cookie(afs_vnode_cache(vnode), true);
0908 
0909     /* Prevent any new writebacks from starting whilst we do this. */
0910     down_write(&vnode->validate_lock);
0911 
0912     if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode)) {
0913         loff_t size = attr->ia_size;
0914 
0915         /* Wait for any outstanding writes to the server to complete */
0916         loff_t from = min(size, i_size);
0917         loff_t to = max(size, i_size);
0918         ret = filemap_fdatawait_range(inode->i_mapping, from, to);
0919         if (ret < 0)
0920             goto out_unlock;
0921 
0922         /* Don't talk to the server if we're just shortening in-memory
0923          * writes that haven't gone to the server yet.
0924          */
0925         if (!(attr->ia_valid & (supported & ~ATTR_SIZE & ~ATTR_MTIME)) &&
0926             attr->ia_size < i_size &&
0927             attr->ia_size > vnode->status.size) {
0928             truncate_pagecache(inode, attr->ia_size);
0929             fscache_resize_cookie(afs_vnode_cache(vnode),
0930                           attr->ia_size);
0931             i_size_write(inode, attr->ia_size);
0932             ret = 0;
0933             goto out_unlock;
0934         }
0935     }
0936 
0937     op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ?
0938                   afs_file_key(attr->ia_file) : NULL),
0939                  vnode->volume);
0940     if (IS_ERR(op)) {
0941         ret = PTR_ERR(op);
0942         goto out_unlock;
0943     }
0944 
0945     afs_op_set_vnode(op, 0, vnode);
0946     op->setattr.attr = attr;
0947 
0948     if (attr->ia_valid & ATTR_SIZE) {
0949         op->file[0].dv_delta = 1;
0950         op->file[0].set_size = true;
0951     }
0952     op->ctime = attr->ia_ctime;
0953     op->file[0].update_ctime = 1;
0954     op->file[0].modification = true;
0955 
0956     op->ops = &afs_setattr_operation;
0957     ret = afs_do_sync_operation(op);
0958 
0959 out_unlock:
0960     up_write(&vnode->validate_lock);
0961     fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
0962     _leave(" = %d", ret);
0963     return ret;
0964 }