Whamcloud - gitweb
LU-13397 lfs: mirror extend/copy keeps sparseness
[fs/lustre-release.git] / lustre / llite / file.c
index 2e78b2d..5791569 100644 (file)
@@ -112,12 +112,12 @@ static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
        op_data->op_xvalid |= OP_XVALID_CTIME_SET;
        op_data->op_attr_blocks = inode->i_blocks;
        op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
-       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
+       if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
                op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
        op_data->op_open_handle = och->och_open_handle;
 
        if (och->och_flags & FMODE_WRITE &&
-           ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
+           test_and_clear_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags))
                /* For HSM: if inode data has been modified, pack it so that
                 * MDT can set data dirty flag in the archive. */
                op_data->op_bias |= MDS_DATA_MODIFIED;
@@ -393,7 +393,7 @@ int ll_file_release(struct inode *inode, struct file *file)
        if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
                ll_deauthorize_statahead(inode, fd);
 
-       if (inode->i_sb->s_root == file_dentry(file)) {
+       if (is_root_inode(inode)) {
                file->private_data = NULL;
                ll_file_data_put(fd);
                GOTO(out, rc = 0);
@@ -413,7 +413,7 @@ int ll_file_release(struct inode *inode, struct file *file)
                libcfs_debug_dumplog();
 
 out:
-       if (!rc && inode->i_sb->s_root != file_dentry(file))
+       if (!rc && !is_root_inode(inode))
                ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
                                   ktime_us_delta(ktime_get(), kstart));
        RETURN(rc);
@@ -730,6 +730,8 @@ static int ll_local_open(struct file *file, struct lookup_intent *it,
        file->private_data = fd;
        ll_readahead_init(inode, &fd->fd_ras);
        fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
+       /* turn off the kernel's read-ahead */
+       file->f_ra.ra_pages = 0;
 
        /* ll_cl_context initialize */
        rwlock_init(&fd->fd_lock);
@@ -783,7 +785,7 @@ int ll_file_open(struct inode *inode, struct file *file)
        if (S_ISDIR(inode->i_mode))
                ll_authorize_statahead(inode, fd);
 
-       if (inode->i_sb->s_root == file_dentry(file)) {
+       if (is_root_inode(inode)) {
                file->private_data = fd;
                RETURN(0);
        }
@@ -1363,7 +1365,7 @@ int ll_merge_attr(const struct lu_env *env, struct inode *inode)
         * POSIX. Solving this problem needs to send an RPC to MDT for each
         * read, this will hurt performance.
         */
-       if (ll_file_test_and_clear_flag(lli, LLIF_UPDATE_ATIME) ||
+       if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
            inode->i_atime.tv_sec < lli->lli_atime)
                inode->i_atime.tv_sec = lli->lli_atime;
 
@@ -1881,7 +1883,7 @@ static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
                ll_heat_add(inode, CIT_WRITE, result);
                ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
                                   result);
-               ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
+               set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
        }
 
        CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
@@ -2241,7 +2243,7 @@ static int ll_lov_setea(struct inode *inode, struct file *file,
        int                      rc;
        ENTRY;
 
-       if (!cfs_capable(CFS_CAP_SYS_ADMIN))
+       if (!capable(CAP_SYS_ADMIN))
                RETURN(-EPERM);
 
        OBD_ALLOC_LARGE(lump, lum_size);
@@ -2379,12 +2381,12 @@ retry:
                        GOTO(out, rc = PTR_ERR(env));
 
                rc = cl_object_layout_get(env, obj, &cl);
-               if (!rc && cl.cl_is_composite)
+               if (rc >= 0 && cl.cl_is_composite)
                        rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
                                                    &ext);
 
                cl_env_put(env, &refcheck);
-               if (rc)
+               if (rc < 0)
                        GOTO(out, rc);
        }
 
@@ -2460,26 +2462,26 @@ out:
  */
 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
 {
-        struct inode *inode = dentry->d_inode;
-        struct obd_client_handle *och;
-        int rc;
-        ENTRY;
+       struct inode *inode = dentry->d_inode;
+       struct obd_client_handle *och;
+       int rc;
+       ENTRY;
 
-        LASSERT(inode);
+       LASSERT(inode);
 
-        /* Root ? Do nothing. */
-        if (dentry->d_inode->i_sb->s_root == dentry)
-                RETURN(0);
+       /* Root ? Do nothing. */
+       if (is_root_inode(inode))
+               RETURN(0);
 
-        /* No open handle to close? Move away */
-        if (!it_disposition(it, DISP_OPEN_OPEN))
-                RETURN(0);
+       /* No open handle to close? Move away */
+       if (!it_disposition(it, DISP_OPEN_OPEN))
+               RETURN(0);
 
-        LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
+       LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
 
-        OBD_ALLOC(och, sizeof(*och));
-        if (!och)
-                GOTO(out, rc = -ENOMEM);
+       OBD_ALLOC(och, sizeof(*och));
+       if (!och)
+               GOTO(out, rc = -ENOMEM);
 
        rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
        if (rc)
@@ -2563,7 +2565,7 @@ int ll_fid2path(struct inode *inode, void __user *arg)
 
        ENTRY;
 
-       if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
+       if (!capable(CAP_DAC_READ_SEARCH) &&
            !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
                RETURN(-EPERM);
 
@@ -2851,7 +2853,7 @@ int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
        /* Non-root users are forbidden to set or clear flags which are
         * NOT defined in HSM_USER_MASK. */
        if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
-           !cfs_capable(CFS_CAP_SYS_ADMIN))
+           !capable(CAP_SYS_ADMIN))
                RETURN(-EPERM);
 
        if (!exp_connect_archive_id_array(exp)) {
@@ -3227,7 +3229,7 @@ int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
                RETURN(-EFAULT);
 
        fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
-       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
+       if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
                fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
        fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
        if (copy_to_user((struct fsxattr __user *)arg,
@@ -3250,7 +3252,7 @@ int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
        if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
                return -EINVAL;
 
-       if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT)) {
+       if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
                if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
                        return -EINVAL;
        } else {
@@ -3264,14 +3266,12 @@ int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
                        unsigned long arg)
 {
-
        struct md_op_data *op_data;
        struct ptlrpc_request *req = NULL;
-       int rc = 0;
        struct fsxattr fsxattr;
        struct cl_object *obj;
-       struct iattr *attr;
-       int flags;
+       unsigned int inode_flags;
+       int rc = 0;
 
        if (copy_from_user(&fsxattr,
                           (const struct fsxattr __user *)arg,
@@ -3287,34 +3287,31 @@ int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
        if (IS_ERR(op_data))
                RETURN(PTR_ERR(op_data));
 
-       flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
-       op_data->op_attr_flags = ll_inode_to_ext_flags(flags);
+       inode_flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
+       op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
        if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
                op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
        op_data->op_projid = fsxattr.fsx_projid;
        op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
-       rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
-                       0, &req);
+       rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
        ptlrpc_req_finished(req);
        if (rc)
                GOTO(out_fsxattr, rc);
        ll_update_inode_flags(inode, op_data->op_attr_flags);
-       obj = ll_i2info(inode)->lli_clob;
-       if (obj == NULL)
-               GOTO(out_fsxattr, rc);
 
-       /* Avoiding OST RPC if this is only project ioctl */
+       /* Avoid OST RPC if this is only ioctl setting project inherit flag */
        if (fsxattr.fsx_xflags == 0 ||
            fsxattr.fsx_xflags == FS_XFLAG_PROJINHERIT)
                GOTO(out_fsxattr, rc);
 
-       OBD_ALLOC_PTR(attr);
-       if (attr == NULL)
-               GOTO(out_fsxattr, rc = -ENOMEM);
+       obj = ll_i2info(inode)->lli_clob;
+       if (obj) {
+               struct iattr attr = { 0 };
+
+               rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS,
+                                   fsxattr.fsx_xflags);
+       }
 
-       rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS,
-                           fsxattr.fsx_xflags);
-       OBD_FREE_PTR(attr);
 out_fsxattr:
        ll_finish_md_op_data(op_data);
        RETURN(rc);
@@ -4071,14 +4068,29 @@ out_state:
                        return -EOPNOTSUPP;
                return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
 #endif
+
+       case LL_IOC_UNLOCK_FOREIGN: {
+               struct dentry *dentry = file_dentry(file);
+
+               /* if not a foreign symlink do nothing */
+               if (ll_foreign_is_removable(dentry, true)) {
+                       CDEBUG(D_INFO,
+                              "prevent unlink of non-foreign file ("DFID")\n",
+                              PFID(ll_inode2fid(inode)));
+                       RETURN(-EOPNOTSUPP);
+               }
+               RETURN(0);
+       }
+
        default:
                RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
                                     (void __user *)arg));
        }
 }
 
-loff_t ll_lseek(struct inode *inode, loff_t offset, int whence)
+loff_t ll_lseek(struct file *file, loff_t offset, int whence)
 {
+       struct inode *inode = file_inode(file);
        struct lu_env *env;
        struct cl_io *io;
        struct cl_lseek_io *lsio;
@@ -4094,6 +4106,7 @@ loff_t ll_lseek(struct inode *inode, loff_t offset, int whence)
 
        io = vvp_env_thread_io(env);
        io->ci_obj = ll_i2info(inode)->lli_clob;
+       ll_io_set_mirror(io, file);
 
        lsio = &io->u.ci_lseek;
        lsio->ls_start = offset;
@@ -4102,10 +4115,14 @@ loff_t ll_lseek(struct inode *inode, loff_t offset, int whence)
 
        do {
                rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
-               if (!rc)
+               if (!rc) {
+                       struct vvp_io *vio = vvp_env_io(env);
+
+                       vio->vui_fd = file->private_data;
                        rc = cl_io_loop(env, io);
-               else
+               } else {
                        rc = io->ci_result;
+               }
                retval = rc ? : lsio->ls_result;
                cl_io_fini(env, io);
        } while (unlikely(io->ci_need_restart));
@@ -4142,7 +4159,7 @@ static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
                cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
                                   CL_FSYNC_LOCAL, 0);
 
-               retval = ll_lseek(inode, offset, origin);
+               retval = ll_lseek(file, offset, origin);
                if (retval < 0)
                        return retval;
                retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
@@ -4544,9 +4561,20 @@ int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
         * by checking the migrate FID against the FID of the
         * filesystem root.
         */
-       if (child_inode == parent->i_sb->s_root->d_inode)
+       if (is_root_inode(child_inode))
                GOTO(out_iput, rc = -EINVAL);
 
+       if (IS_ENCRYPTED(child_inode)) {
+               rc = llcrypt_get_encryption_info(child_inode);
+               if (rc)
+                       GOTO(out_iput, rc);
+               if (!llcrypt_has_encryption_key(child_inode)) {
+                       CDEBUG(D_SEC, "no enc key for "DFID"\n",
+                              PFID(ll_inode2fid(child_inode)));
+                       GOTO(out_iput, rc = -ENOKEY);
+               }
+       }
+
        op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
                                     child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
        if (IS_ERR(op_data))
@@ -4839,7 +4867,7 @@ static int ll_merge_md_attr(struct inode *inode)
 }
 
 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
-                     unsigned int flags)
+                     unsigned int flags, bool foreign)
 {
        struct inode *inode = de->d_inode;
        struct ll_sb_info *sbi = ll_i2sbi(inode);
@@ -4865,7 +4893,10 @@ int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
        if (rc < 0)
                RETURN(rc);
 
-       if (S_ISREG(inode->i_mode)) {
+       /* foreign file/dir are always of zero length, so don't
+        * need to validate size.
+        */
+       if (S_ISREG(inode->i_mode) && !foreign) {
                bool cached;
 
                if (!need_glimpse)
@@ -4905,14 +4936,15 @@ int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
                 * restore the MDT holds the layout lock so the glimpse will
                 * block up to the end of restore (getattr will block)
                 */
-               if (!ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
+               if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
                        rc = ll_glimpse_size(inode);
                        if (rc < 0)
                                RETURN(rc);
                }
        } else {
                /* If object isn't regular a file then don't validate size. */
-               if (ll_dir_striped(inode)) {
+               /* foreign dir is not striped dir */
+               if (ll_dir_striped(inode) && !foreign) {
                        rc = ll_merge_md_attr(inode);
                        if (rc < 0)
                                RETURN(rc);
@@ -4939,7 +4971,12 @@ fill_attr:
                stat->rdev = inode->i_rdev;
        }
 
-       stat->mode = inode->i_mode;
+       /* foreign symlink to be exposed as a real symlink */
+       if (!foreign)
+               stat->mode = inode->i_mode;
+       else
+               stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
+
        stat->uid = inode->i_uid;
        stat->gid = inode->i_gid;
        stat->atime = inode->i_atime;
@@ -4988,13 +5025,14 @@ fill_attr:
 int ll_getattr(const struct path *path, struct kstat *stat,
               u32 request_mask, unsigned int flags)
 {
-       return ll_getattr_dentry(path->dentry, stat, request_mask, flags);
+       return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
+                                false);
 }
 #else
 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
 {
        return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
-                                AT_STATX_SYNC_AS_STAT);
+                                AT_STATX_SYNC_AS_STAT, false);
 }
 #endif
 
@@ -5142,80 +5180,6 @@ out:
        return rc;
 }
 
-struct posix_acl *ll_get_acl(struct inode *inode, int type)
-{
-       struct ll_inode_info *lli = ll_i2info(inode);
-       struct posix_acl *acl = NULL;
-       ENTRY;
-
-       spin_lock(&lli->lli_lock);
-       /* VFS' acl_permission_check->check_acl will release the refcount */
-       acl = posix_acl_dup(lli->lli_posix_acl);
-       spin_unlock(&lli->lli_lock);
-
-       RETURN(acl);
-}
-
-#ifdef HAVE_IOP_SET_ACL
-#ifdef CONFIG_LUSTRE_FS_POSIX_ACL
-int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
-{
-       struct ll_sb_info *sbi = ll_i2sbi(inode);
-       struct ptlrpc_request *req = NULL;
-       const char *name = NULL;
-       char *value = NULL;
-       size_t value_size = 0;
-       int rc = 0;
-       ENTRY;
-
-       switch (type) {
-       case ACL_TYPE_ACCESS:
-               name = XATTR_NAME_POSIX_ACL_ACCESS;
-               if (acl)
-                       rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
-               break;
-
-       case ACL_TYPE_DEFAULT:
-               name = XATTR_NAME_POSIX_ACL_DEFAULT;
-               if (!S_ISDIR(inode->i_mode))
-                       rc = acl ? -EACCES : 0;
-               break;
-
-       default:
-               rc = -EINVAL;
-               break;
-       }
-       if (rc)
-               return rc;
-
-       if (acl) {
-               value_size = posix_acl_xattr_size(acl->a_count);
-               value = kmalloc(value_size, GFP_NOFS);
-               if (value == NULL)
-                       GOTO(out, rc = -ENOMEM);
-
-               rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
-               if (rc < 0)
-                       GOTO(out_value, rc);
-       }
-
-       rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
-                        value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
-                        name, value, value_size, 0, 0, &req);
-
-       ptlrpc_req_finished(req);
-out_value:
-       kfree(value);
-out:
-       if (rc)
-               forget_cached_acl(inode, type);
-       else
-               set_cached_acl(inode, type, acl);
-       RETURN(rc);
-}
-#endif /* CONFIG_LUSTRE_FS_POSIX_ACL */
-#endif /* HAVE_IOP_SET_ACL */
-
 int ll_inode_permission(struct inode *inode, int mask)
 {
        int rc = 0;
@@ -5237,7 +5201,7 @@ int ll_inode_permission(struct inode *inode, int mask)
         * need to do it before permission check.
         */
 
-       if (inode == inode->i_sb->s_root->d_inode) {
+       if (is_root_inode(inode)) {
                rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
                if (rc)
                        RETURN(rc);
@@ -5289,7 +5253,7 @@ int ll_inode_permission(struct inode *inode, int mask)
 }
 
 /* -o localflock - only provides locally consistent flock locks */
-struct file_operations ll_file_operations = {
+static const struct file_operations ll_file_operations = {
 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
 # ifdef HAVE_SYNC_READ_WRITE
        .read           = new_sync_read,
@@ -5318,7 +5282,7 @@ struct file_operations ll_file_operations = {
        .fallocate      = ll_fallocate,
 };
 
-struct file_operations ll_file_operations_flock = {
+static const struct file_operations ll_file_operations_flock = {
 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
 # ifdef HAVE_SYNC_READ_WRITE
        .read           = new_sync_read,
@@ -5350,7 +5314,7 @@ struct file_operations ll_file_operations_flock = {
 };
 
 /* These are for -o noflock - to return ENOSYS on flock calls */
-struct file_operations ll_file_operations_noflock = {
+static const struct file_operations ll_file_operations_noflock = {
 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
 # ifdef HAVE_SYNC_READ_WRITE
        .read           = new_sync_read,
@@ -5381,7 +5345,7 @@ struct file_operations ll_file_operations_noflock = {
        .fallocate      = ll_fallocate,
 };
 
-struct inode_operations ll_file_inode_operations = {
+const struct inode_operations ll_file_inode_operations = {
        .setattr        = ll_setattr,
        .getattr        = ll_getattr,
        .permission     = ll_inode_permission,
@@ -5398,6 +5362,18 @@ struct inode_operations ll_file_inode_operations = {
 #endif
 };
 
+const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
+{
+       const struct file_operations *fops = &ll_file_operations_noflock;
+
+       if (sbi->ll_flags & LL_SBI_FLOCK)
+               fops = &ll_file_operations_flock;
+       else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
+               fops = &ll_file_operations;
+
+       return fops;
+}
+
 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
 {
        struct ll_inode_info *lli = ll_i2info(inode);
@@ -5447,7 +5423,7 @@ int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
 out:
        cl_env_put(env, &refcheck);
 
-       RETURN(rc);
+       RETURN(rc < 0 ? rc : 0);
 }
 
 /* Fetch layout from MDT with getxattr request, if it's not ready yet */