Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPL HEADER START |
| 3 | * |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License version 2 for more details (a copy is included |
| 14 | * in the LICENSE file that accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 along with this program; If not, see |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | * |
Andreas Dilger | 1dc563a | 2015-11-08 18:09:37 -0500 | [diff] [blame] | 26 | * Copyright (c) 2011, 2015, Intel Corporation. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 27 | */ |
| 28 | /* |
| 29 | * This file is part of Lustre, http://www.lustre.org/ |
| 30 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 31 | * |
| 32 | * lustre/llite/file.c |
| 33 | * |
| 34 | * Author: Peter Braam <braam@clusterfs.com> |
| 35 | * Author: Phil Schwan <phil@clusterfs.com> |
| 36 | * Author: Andreas Dilger <adilger@clusterfs.com> |
| 37 | */ |
| 38 | |
| 39 | #define DEBUG_SUBSYSTEM S_LLITE |
Greg Kroah-Hartman | 67a235f | 2014-07-11 21:51:41 -0700 | [diff] [blame] | 40 | #include "../include/lustre_dlm.h" |
| 41 | #include "../include/lustre_lite.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 42 | #include <linux/pagemap.h> |
| 43 | #include <linux/file.h> |
Oleg Drokin | bb41292 | 2016-03-30 19:48:38 -0400 | [diff] [blame] | 44 | #include <linux/mount.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 45 | #include "llite_internal.h" |
Greg Kroah-Hartman | 67a235f | 2014-07-11 21:51:41 -0700 | [diff] [blame] | 46 | #include "../include/lustre/ll_fiemap.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 47 | |
Greg Kroah-Hartman | 67a235f | 2014-07-11 21:51:41 -0700 | [diff] [blame] | 48 | #include "../include/cl_object.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 49 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 50 | static int |
| 51 | ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg); |
| 52 | |
| 53 | static int ll_lease_close(struct obd_client_handle *och, struct inode *inode, |
| 54 | bool *lease_broken); |
| 55 | |
| 56 | static enum llioc_iter |
| 57 | ll_iocontrol_call(struct inode *inode, struct file *file, |
| 58 | unsigned int cmd, unsigned long arg, int *rcp); |
| 59 | |
| 60 | static struct ll_file_data *ll_file_data_get(void) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 61 | { |
| 62 | struct ll_file_data *fd; |
| 63 | |
Amitoj Kaur Chawla | 21068c4 | 2016-02-26 14:25:09 +0530 | [diff] [blame] | 64 | fd = kmem_cache_zalloc(ll_file_data_slab, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 65 | if (!fd) |
John L. Hammond | 73863d83 | 2013-07-23 00:06:27 +0800 | [diff] [blame] | 66 | return NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 67 | fd->fd_write_failed = false; |
| 68 | return fd; |
| 69 | } |
| 70 | |
| 71 | static void ll_file_data_put(struct ll_file_data *fd) |
| 72 | { |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 73 | if (fd) |
Mike Rapoport | 50d3036 | 2015-10-20 12:39:51 +0300 | [diff] [blame] | 74 | kmem_cache_free(ll_file_data_slab, fd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data, |
| 78 | struct lustre_handle *fh) |
| 79 | { |
| 80 | op_data->op_fid1 = ll_i2info(inode)->lli_fid; |
| 81 | op_data->op_attr.ia_mode = inode->i_mode; |
| 82 | op_data->op_attr.ia_atime = inode->i_atime; |
| 83 | op_data->op_attr.ia_mtime = inode->i_mtime; |
| 84 | op_data->op_attr.ia_ctime = inode->i_ctime; |
| 85 | op_data->op_attr.ia_size = i_size_read(inode); |
| 86 | op_data->op_attr_blocks = inode->i_blocks; |
Oleg Drokin | bb41292 | 2016-03-30 19:48:38 -0400 | [diff] [blame] | 87 | op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 88 | op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch; |
| 89 | if (fh) |
| 90 | op_data->op_handle = *fh; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 91 | |
Julia Lawall | 1f6eaf8 | 2015-08-29 19:30:09 +0200 | [diff] [blame] | 92 | if (ll_i2info(inode)->lli_flags & LLIF_DATA_MODIFIED) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 93 | op_data->op_bias |= MDS_DATA_MODIFIED; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Closes the IO epoch and packs all the attributes into @op_data for |
| 98 | * the CLOSE rpc. |
| 99 | */ |
| 100 | static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data, |
| 101 | struct obd_client_handle *och) |
| 102 | { |
Emoly Liu | f57d9a7 | 2013-06-03 21:40:55 +0800 | [diff] [blame] | 103 | op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET | |
| 104 | ATTR_MTIME | ATTR_MTIME_SET | |
| 105 | ATTR_CTIME | ATTR_CTIME_SET; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 106 | |
| 107 | if (!(och->och_flags & FMODE_WRITE)) |
| 108 | goto out; |
| 109 | |
| 110 | if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode)) |
| 111 | op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS; |
| 112 | else |
| 113 | ll_ioepoch_close(inode, op_data, &och, 0); |
| 114 | |
| 115 | out: |
| 116 | ll_pack_inode2opdata(inode, op_data, &och->och_fh); |
| 117 | ll_prep_md_op_data(op_data, inode, NULL, NULL, |
| 118 | 0, 0, LUSTRE_OPC_ANY, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int ll_close_inode_openhandle(struct obd_export *md_exp, |
| 122 | struct inode *inode, |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 123 | struct obd_client_handle *och, |
| 124 | const __u64 *data_version) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 125 | { |
| 126 | struct obd_export *exp = ll_i2mdexp(inode); |
| 127 | struct md_op_data *op_data; |
| 128 | struct ptlrpc_request *req = NULL; |
| 129 | struct obd_device *obd = class_exp2obd(exp); |
| 130 | int epoch_close = 1; |
| 131 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 132 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 133 | if (!obd) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 134 | /* |
| 135 | * XXX: in case of LMV, is this correct to access |
| 136 | * ->exp_handle? |
| 137 | */ |
Greg Kroah-Hartman | 55f5a82 | 2014-07-12 20:26:07 -0700 | [diff] [blame] | 138 | CERROR("Invalid MDC connection handle %#llx\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 139 | ll_i2mdexp(inode)->exp_handle.h_cookie); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 140 | rc = 0; |
| 141 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 142 | } |
| 143 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 144 | op_data = kzalloc(sizeof(*op_data), GFP_NOFS); |
| 145 | if (!op_data) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 146 | /* XXX We leak openhandle and request here. */ |
| 147 | rc = -ENOMEM; |
| 148 | goto out; |
| 149 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 150 | |
| 151 | ll_prepare_close(inode, op_data, och); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 152 | if (data_version) { |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 153 | /* Pass in data_version implies release. */ |
| 154 | op_data->op_bias |= MDS_HSM_RELEASE; |
| 155 | op_data->op_data_version = *data_version; |
| 156 | op_data->op_lease_handle = och->och_lease_handle; |
| 157 | op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS; |
| 158 | } |
Haneen Mohammed | b6ee382 | 2015-03-13 20:48:53 +0300 | [diff] [blame] | 159 | epoch_close = op_data->op_flags & MF_EPOCH_CLOSE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 160 | rc = md_close(md_exp, op_data, och->och_mod, &req); |
| 161 | if (rc == -EAGAIN) { |
| 162 | /* This close must have the epoch closed. */ |
| 163 | LASSERT(epoch_close); |
| 164 | /* MDS has instructed us to obtain Size-on-MDS attribute from |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 165 | * OSTs and send setattr to back to MDS. |
| 166 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 167 | rc = ll_som_update(inode, op_data); |
| 168 | if (rc) { |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 169 | CERROR("%s: inode "DFID" mdc Size-on-MDS update failed: rc = %d\n", |
| 170 | ll_i2mdexp(inode)->exp_obd->obd_name, |
| 171 | PFID(ll_inode2fid(inode)), rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 172 | rc = 0; |
| 173 | } |
| 174 | } else if (rc) { |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 175 | CERROR("%s: inode "DFID" mdc close failed: rc = %d\n", |
| 176 | ll_i2mdexp(inode)->exp_obd->obd_name, |
| 177 | PFID(ll_inode2fid(inode)), rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* DATA_MODIFIED flag was successfully sent on close, cancel data |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 181 | * modification flag. |
| 182 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 183 | if (rc == 0 && (op_data->op_bias & MDS_DATA_MODIFIED)) { |
| 184 | struct ll_inode_info *lli = ll_i2info(inode); |
| 185 | |
| 186 | spin_lock(&lli->lli_lock); |
| 187 | lli->lli_flags &= ~LLIF_DATA_MODIFIED; |
| 188 | spin_unlock(&lli->lli_lock); |
| 189 | } |
| 190 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 191 | if (rc == 0) { |
| 192 | rc = ll_objects_destroy(req, inode); |
| 193 | if (rc) |
| 194 | CERROR("inode %lu ll_objects destroy: rc = %d\n", |
| 195 | inode->i_ino, rc); |
| 196 | } |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 197 | if (rc == 0 && op_data->op_bias & MDS_HSM_RELEASE) { |
| 198 | struct mdt_body *body; |
aybuke ozdemir | cea812c | 2015-02-26 23:45:45 +0200 | [diff] [blame] | 199 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 200 | body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY); |
| 201 | if (!(body->valid & OBD_MD_FLRELEASED)) |
| 202 | rc = -EBUSY; |
| 203 | } |
| 204 | |
| 205 | ll_finish_md_op_data(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 206 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 207 | out: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 208 | if (exp_connect_som(exp) && !epoch_close && |
| 209 | S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) { |
| 210 | ll_queue_done_writing(inode, LLIF_DONE_WRITING); |
| 211 | } else { |
| 212 | md_clear_open_replay_data(md_exp, och); |
| 213 | /* Free @och if it is not waiting for DONE_WRITING. */ |
| 214 | och->och_fh.cookie = DEAD_HANDLE_MAGIC; |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 215 | kfree(och); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 216 | } |
| 217 | if (req) /* This is close request */ |
| 218 | ptlrpc_req_finished(req); |
| 219 | return rc; |
| 220 | } |
| 221 | |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 222 | int ll_md_real_close(struct inode *inode, fmode_t fmode) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 223 | { |
| 224 | struct ll_inode_info *lli = ll_i2info(inode); |
| 225 | struct obd_client_handle **och_p; |
| 226 | struct obd_client_handle *och; |
| 227 | __u64 *och_usecount; |
| 228 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 229 | |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 230 | if (fmode & FMODE_WRITE) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 231 | och_p = &lli->lli_mds_write_och; |
| 232 | och_usecount = &lli->lli_open_fd_write_count; |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 233 | } else if (fmode & FMODE_EXEC) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 234 | och_p = &lli->lli_mds_exec_och; |
| 235 | och_usecount = &lli->lli_open_fd_exec_count; |
| 236 | } else { |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 237 | LASSERT(fmode & FMODE_READ); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 238 | och_p = &lli->lli_mds_read_och; |
| 239 | och_usecount = &lli->lli_open_fd_read_count; |
| 240 | } |
| 241 | |
| 242 | mutex_lock(&lli->lli_och_mutex); |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 243 | if (*och_usecount > 0) { |
| 244 | /* There are still users of this handle, so skip |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 245 | * freeing it. |
| 246 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 247 | mutex_unlock(&lli->lli_och_mutex); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 248 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 249 | } |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 250 | |
Tina Ruchandani | 57303e7 | 2014-10-21 22:13:21 -0700 | [diff] [blame] | 251 | och = *och_p; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 252 | *och_p = NULL; |
| 253 | mutex_unlock(&lli->lli_och_mutex); |
| 254 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 255 | if (och) { |
John L. Hammond | 45b2a01 | 2014-02-28 21:16:30 -0500 | [diff] [blame] | 256 | /* There might be a race and this handle may already |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 257 | * be closed. |
| 258 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 259 | rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 260 | inode, och, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 263 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 264 | } |
| 265 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 266 | static int ll_md_close(struct obd_export *md_exp, struct inode *inode, |
| 267 | struct file *file) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 268 | { |
| 269 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 270 | struct ll_inode_info *lli = ll_i2info(inode); |
Al Viro | 74d0195 | 2014-05-07 20:55:28 -0400 | [diff] [blame] | 271 | int lockmode; |
| 272 | __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK; |
| 273 | struct lustre_handle lockh; |
Anjali Menon | 8369cff | 2015-08-18 19:24:24 +0530 | [diff] [blame] | 274 | ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_OPEN} }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 275 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 276 | |
| 277 | /* clear group lock, if present */ |
| 278 | if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED)) |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 279 | ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 280 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 281 | if (fd->fd_lease_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 282 | bool lease_broken; |
| 283 | |
| 284 | /* Usually the lease is not released when the |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 285 | * application crashed, we need to release here. |
| 286 | */ |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 287 | rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken); |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 288 | CDEBUG(rc ? D_ERROR : D_INODE, |
| 289 | "Clean up lease " DFID " %d/%d\n", |
| 290 | PFID(&lli->lli_fid), rc, lease_broken); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 291 | |
| 292 | fd->fd_lease_och = NULL; |
| 293 | } |
| 294 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 295 | if (fd->fd_och) { |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 296 | rc = ll_close_inode_openhandle(md_exp, inode, fd->fd_och, NULL); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 297 | fd->fd_och = NULL; |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 298 | goto out; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 299 | } |
| 300 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 301 | /* Let's see if we have good enough OPEN lock on the file and if |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 302 | * we can skip talking to MDS |
| 303 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 304 | |
Al Viro | 74d0195 | 2014-05-07 20:55:28 -0400 | [diff] [blame] | 305 | mutex_lock(&lli->lli_och_mutex); |
| 306 | if (fd->fd_omode & FMODE_WRITE) { |
| 307 | lockmode = LCK_CW; |
| 308 | LASSERT(lli->lli_open_fd_write_count); |
| 309 | lli->lli_open_fd_write_count--; |
| 310 | } else if (fd->fd_omode & FMODE_EXEC) { |
| 311 | lockmode = LCK_PR; |
| 312 | LASSERT(lli->lli_open_fd_exec_count); |
| 313 | lli->lli_open_fd_exec_count--; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 314 | } else { |
Al Viro | 74d0195 | 2014-05-07 20:55:28 -0400 | [diff] [blame] | 315 | lockmode = LCK_CR; |
| 316 | LASSERT(lli->lli_open_fd_read_count); |
| 317 | lli->lli_open_fd_read_count--; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 318 | } |
Al Viro | 74d0195 | 2014-05-07 20:55:28 -0400 | [diff] [blame] | 319 | mutex_unlock(&lli->lli_och_mutex); |
| 320 | |
| 321 | if (!md_lock_match(md_exp, flags, ll_inode2fid(inode), |
| 322 | LDLM_IBITS, &policy, lockmode, &lockh)) |
| 323 | rc = ll_md_real_close(inode, fd->fd_omode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 324 | |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 325 | out: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 326 | LUSTRE_FPRIVATE(file) = NULL; |
| 327 | ll_file_data_put(fd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 328 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 329 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* While this returns an error code, fput() the caller does not, so we need |
| 333 | * to make every effort to clean up all of our state here. Also, applications |
| 334 | * rarely check close errors and even if an error is returned they will not |
| 335 | * re-try the close call. |
| 336 | */ |
| 337 | int ll_file_release(struct inode *inode, struct file *file) |
| 338 | { |
| 339 | struct ll_file_data *fd; |
| 340 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 341 | struct ll_inode_info *lli = ll_i2info(inode); |
| 342 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 343 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 344 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n", |
| 345 | PFID(ll_inode2fid(inode)), inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 346 | |
Al Viro | f76c23d | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 347 | if (!is_root_inode(inode)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 348 | ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1); |
| 349 | fd = LUSTRE_FPRIVATE(file); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 350 | LASSERT(fd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 351 | |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 352 | /* The last ref on @file, maybe not be the owner pid of statahead. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 353 | * Different processes can open the same dir, "ll_opendir_key" means: |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 354 | * it is me that should stop the statahead thread. |
| 355 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 356 | if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd && |
| 357 | lli->lli_opendir_pid != 0) |
| 358 | ll_stop_statahead(inode, lli->lli_opendir_key); |
| 359 | |
Al Viro | f76c23d | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 360 | if (is_root_inode(inode)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 361 | LUSTRE_FPRIVATE(file) = NULL; |
| 362 | ll_file_data_put(fd); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 363 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (!S_ISDIR(inode->i_mode)) { |
| 367 | lov_read_and_clear_async_rc(lli->lli_clob); |
| 368 | lli->lli_async_rc = 0; |
| 369 | } |
| 370 | |
| 371 | rc = ll_md_close(sbi->ll_md_exp, inode, file); |
| 372 | |
| 373 | if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val)) |
| 374 | libcfs_debug_dumplog(); |
| 375 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 376 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 379 | static int ll_intent_file_open(struct dentry *dentry, void *lmm, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 380 | int lmmsize, struct lookup_intent *itp) |
| 381 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 382 | struct inode *inode = d_inode(dentry); |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 383 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 384 | struct dentry *parent = dentry->d_parent; |
| 385 | const char *name = dentry->d_name.name; |
| 386 | const int len = dentry->d_name.len; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 387 | struct md_op_data *op_data; |
| 388 | struct ptlrpc_request *req; |
| 389 | __u32 opc = LUSTRE_OPC_ANY; |
| 390 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 391 | |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 392 | /* Usually we come here only for NFSD, and we want open lock. */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 393 | /* We can also get here if there was cached open handle in revalidate_it |
| 394 | * but it disappeared while we were getting from there to ll_file_open. |
Masanari Iida | bef31c7 | 2013-09-15 14:38:18 +0900 | [diff] [blame] | 395 | * But this means this file was closed and immediately opened which |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 396 | * makes a good candidate for using OPEN lock |
| 397 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 398 | /* If lmmsize & lmm are not 0, we are just setting stripe info |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 399 | * parameters. No need for the open lock |
| 400 | */ |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 401 | if (!lmm && lmmsize == 0) { |
Oleg Drokin | 6dad4d8 | 2016-06-20 16:55:45 -0400 | [diff] [blame] | 402 | struct ll_dentry_data *ldd = ll_d2d(dentry); |
| 403 | /* |
| 404 | * If we came via ll_iget_for_nfs, then we need to request |
| 405 | * struct ll_dentry_data *ldd = ll_d2d(file->f_dentry); |
| 406 | * |
| 407 | * NB: when ldd is NULL, it must have come via normal |
| 408 | * lookup path only, since ll_iget_for_nfs always calls |
| 409 | * ll_d_init(). |
| 410 | */ |
| 411 | if (ldd && ldd->lld_nfs_dentry) { |
| 412 | ldd->lld_nfs_dentry = 0; |
| 413 | itp->it_flags |= MDS_OPEN_LOCK; |
| 414 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 415 | if (itp->it_flags & FMODE_WRITE) |
| 416 | opc = LUSTRE_OPC_CREATE; |
| 417 | } |
| 418 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 419 | op_data = ll_prep_md_op_data(NULL, d_inode(parent), |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 420 | inode, name, len, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 421 | O_RDWR, opc, NULL); |
| 422 | if (IS_ERR(op_data)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 423 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 424 | |
| 425 | itp->it_flags |= MDS_OPEN_BY_FID; |
| 426 | rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp, |
| 427 | 0 /*unused */, &req, ll_md_blocking_ast, 0); |
| 428 | ll_finish_md_op_data(op_data); |
| 429 | if (rc == -ESTALE) { |
| 430 | /* reason for keep own exit path - don`t flood log |
| 431 | * with messages with -ESTALE errors. |
| 432 | */ |
| 433 | if (!it_disposition(itp, DISP_OPEN_OPEN) || |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 434 | it_open_error(DISP_OPEN_OPEN, itp)) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 435 | goto out; |
Al Viro | e22fdcc | 2014-10-20 20:39:57 -0400 | [diff] [blame] | 436 | ll_release_openhandle(inode, itp); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 437 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 440 | if (it_disposition(itp, DISP_LOOKUP_NEG)) { |
| 441 | rc = -ENOENT; |
| 442 | goto out; |
| 443 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 444 | |
| 445 | if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) { |
| 446 | rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp); |
| 447 | CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 448 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 449 | } |
| 450 | |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 451 | rc = ll_prep_inode(&inode, req, NULL, itp); |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 452 | if (!rc && itp->it_lock_mode) |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 453 | ll_set_lock_data(sbi->ll_md_exp, inode, itp, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 454 | |
| 455 | out: |
Lai Siyao | f236f69 | 2014-02-28 21:16:38 -0500 | [diff] [blame] | 456 | ptlrpc_req_finished(req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 457 | ll_intent_drop_lock(itp); |
| 458 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 459 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does |
| 464 | * not believe attributes if a few ioepoch holders exist. Attributes for |
| 465 | * previous ioepoch if new one is opened are also skipped by MDS. |
| 466 | */ |
| 467 | void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch) |
| 468 | { |
| 469 | if (ioepoch && lli->lli_ioepoch != ioepoch) { |
| 470 | lli->lli_ioepoch = ioepoch; |
Greg Kroah-Hartman | b0f5aad | 2014-07-12 20:06:04 -0700 | [diff] [blame] | 471 | CDEBUG(D_INODE, "Epoch %llu opened on "DFID"\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 472 | ioepoch, PFID(&lli->lli_fid)); |
| 473 | } |
| 474 | } |
| 475 | |
John L. Hammond | ea1db08 | 2013-11-15 00:13:08 +0800 | [diff] [blame] | 476 | static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it, |
| 477 | struct obd_client_handle *och) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 478 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 479 | struct mdt_body *body; |
| 480 | |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 481 | body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY); |
John L. Hammond | ea1db08 | 2013-11-15 00:13:08 +0800 | [diff] [blame] | 482 | och->och_fh = body->handle; |
| 483 | och->och_fid = body->fid1; |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 484 | och->och_lease_handle.cookie = it->it_lock_handle; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 485 | och->och_magic = OBD_CLIENT_HANDLE_MAGIC; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 486 | och->och_flags = it->it_flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 487 | |
Hongchao Zhang | 63d4257 | 2014-02-28 21:16:37 -0500 | [diff] [blame] | 488 | return md_set_open_replay_data(md_exp, och, it); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 489 | } |
| 490 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 491 | static int ll_local_open(struct file *file, struct lookup_intent *it, |
| 492 | struct ll_file_data *fd, struct obd_client_handle *och) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 493 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 494 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 495 | struct ll_inode_info *lli = ll_i2info(inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 496 | |
| 497 | LASSERT(!LUSTRE_FPRIVATE(file)); |
| 498 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 499 | LASSERT(fd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 500 | |
| 501 | if (och) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 502 | struct mdt_body *body; |
| 503 | int rc; |
| 504 | |
John L. Hammond | ea1db08 | 2013-11-15 00:13:08 +0800 | [diff] [blame] | 505 | rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och); |
| 506 | if (rc != 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 507 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 508 | |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 509 | body = req_capsule_server_get(&it->it_request->rq_pill, |
| 510 | &RMF_MDT_BODY); |
John L. Hammond | ea1db08 | 2013-11-15 00:13:08 +0800 | [diff] [blame] | 511 | ll_ioepoch_open(lli, body->ioepoch); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | LUSTRE_FPRIVATE(file) = fd; |
| 515 | ll_readahead_init(inode, &fd->fd_ras); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 516 | fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); |
Jinshan Xiong | 966c4a8 | 2016-06-05 23:28:51 -0400 | [diff] [blame] | 517 | |
| 518 | /* ll_cl_context initialize */ |
| 519 | rwlock_init(&fd->fd_lock); |
| 520 | INIT_LIST_HEAD(&fd->fd_lccs); |
| 521 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 522 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* Open a file, and (for the very first open) create objects on the OSTs at |
| 526 | * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object |
| 527 | * creation or open until ll_lov_setstripe() ioctl is called. |
| 528 | * |
| 529 | * If we already have the stripe MD locally then we don't request it in |
| 530 | * md_open(), by passing a lmm_size = 0. |
| 531 | * |
| 532 | * It is up to the application to ensure no other processes open this file |
| 533 | * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be |
| 534 | * used. We might be able to avoid races of that sort by getting lli_open_sem |
| 535 | * before returning in the O_LOV_DELAY_CREATE case and dropping it here |
| 536 | * or in ll_file_release(), but I'm not sure that is desirable/necessary. |
| 537 | */ |
| 538 | int ll_file_open(struct inode *inode, struct file *file) |
| 539 | { |
| 540 | struct ll_inode_info *lli = ll_i2info(inode); |
| 541 | struct lookup_intent *it, oit = { .it_op = IT_OPEN, |
| 542 | .it_flags = file->f_flags }; |
| 543 | struct obd_client_handle **och_p = NULL; |
| 544 | __u64 *och_usecount = NULL; |
| 545 | struct ll_file_data *fd; |
| 546 | int rc = 0, opendir_set = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 547 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 548 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n", |
| 549 | PFID(ll_inode2fid(inode)), inode, file->f_flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 550 | |
| 551 | it = file->private_data; /* XXX: compat macro */ |
| 552 | file->private_data = NULL; /* prevent ll_local_open assertion */ |
| 553 | |
| 554 | fd = ll_file_data_get(); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 555 | if (!fd) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 556 | rc = -ENOMEM; |
| 557 | goto out_openerr; |
| 558 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 559 | |
| 560 | fd->fd_file = file; |
| 561 | if (S_ISDIR(inode->i_mode)) { |
| 562 | spin_lock(&lli->lli_sa_lock); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 563 | if (!lli->lli_opendir_key && !lli->lli_sai && |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 564 | lli->lli_opendir_pid == 0) { |
| 565 | lli->lli_opendir_key = fd; |
| 566 | lli->lli_opendir_pid = current_pid(); |
| 567 | opendir_set = 1; |
| 568 | } |
| 569 | spin_unlock(&lli->lli_sa_lock); |
| 570 | } |
| 571 | |
Al Viro | f76c23d | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 572 | if (is_root_inode(inode)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 573 | LUSTRE_FPRIVATE(file) = fd; |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 574 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 575 | } |
| 576 | |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 577 | if (!it || !it->it_disposition) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 578 | /* Convert f_flags into access mode. We cannot use file->f_mode, |
| 579 | * because everything but O_ACCMODE mask was stripped from |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 580 | * there |
| 581 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 582 | if ((oit.it_flags + 1) & O_ACCMODE) |
| 583 | oit.it_flags++; |
| 584 | if (file->f_flags & O_TRUNC) |
| 585 | oit.it_flags |= FMODE_WRITE; |
| 586 | |
| 587 | /* kernel only call f_op->open in dentry_open. filp_open calls |
| 588 | * dentry_open after call to open_namei that checks permissions. |
| 589 | * Only nfsd_open call dentry_open directly without checking |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 590 | * permissions and because of that this code below is safe. |
| 591 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 592 | if (oit.it_flags & (FMODE_WRITE | FMODE_READ)) |
| 593 | oit.it_flags |= MDS_OPEN_OWNEROVERRIDE; |
| 594 | |
| 595 | /* We do not want O_EXCL here, presumably we opened the file |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 596 | * already? XXX - NFS implications? |
| 597 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 598 | oit.it_flags &= ~O_EXCL; |
| 599 | |
| 600 | /* bug20584, if "it_flags" contains O_CREAT, the file will be |
| 601 | * created if necessary, then "IT_CREAT" should be set to keep |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 602 | * consistent with it |
| 603 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 604 | if (oit.it_flags & O_CREAT) |
| 605 | oit.it_op |= IT_CREAT; |
| 606 | |
| 607 | it = &oit; |
| 608 | } |
| 609 | |
| 610 | restart: |
| 611 | /* Let's see if we have file open on MDS already. */ |
| 612 | if (it->it_flags & FMODE_WRITE) { |
| 613 | och_p = &lli->lli_mds_write_och; |
| 614 | och_usecount = &lli->lli_open_fd_write_count; |
| 615 | } else if (it->it_flags & FMODE_EXEC) { |
| 616 | och_p = &lli->lli_mds_exec_och; |
| 617 | och_usecount = &lli->lli_open_fd_exec_count; |
| 618 | } else { |
| 619 | och_p = &lli->lli_mds_read_och; |
| 620 | och_usecount = &lli->lli_open_fd_read_count; |
| 621 | } |
| 622 | |
| 623 | mutex_lock(&lli->lli_och_mutex); |
| 624 | if (*och_p) { /* Open handle is present */ |
| 625 | if (it_disposition(it, DISP_OPEN_OPEN)) { |
| 626 | /* Well, there's extra open request that we do not need, |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 627 | * let's close it somehow. This will decref request. |
| 628 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 629 | rc = it_open_error(DISP_OPEN_OPEN, it); |
| 630 | if (rc) { |
| 631 | mutex_unlock(&lli->lli_och_mutex); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 632 | goto out_openerr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 633 | } |
| 634 | |
Al Viro | e22fdcc | 2014-10-20 20:39:57 -0400 | [diff] [blame] | 635 | ll_release_openhandle(inode, it); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 636 | } |
| 637 | (*och_usecount)++; |
| 638 | |
| 639 | rc = ll_local_open(file, it, fd, NULL); |
| 640 | if (rc) { |
| 641 | (*och_usecount)--; |
| 642 | mutex_unlock(&lli->lli_och_mutex); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 643 | goto out_openerr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 644 | } |
| 645 | } else { |
| 646 | LASSERT(*och_usecount == 0); |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 647 | if (!it->it_disposition) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 648 | /* We cannot just request lock handle now, new ELC code |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 649 | * means that one of other OPEN locks for this file |
| 650 | * could be cancelled, and since blocking ast handler |
| 651 | * would attempt to grab och_mutex as well, that would |
| 652 | * result in a deadlock |
| 653 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 654 | mutex_unlock(&lli->lli_och_mutex); |
| 655 | it->it_create_mode |= M_CHECK_STALE; |
Al Viro | 48eddfd | 2014-05-07 21:24:47 -0400 | [diff] [blame] | 656 | rc = ll_intent_file_open(file->f_path.dentry, NULL, 0, it); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 657 | it->it_create_mode &= ~M_CHECK_STALE; |
| 658 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 659 | goto out_openerr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 660 | |
| 661 | goto restart; |
| 662 | } |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 663 | *och_p = kzalloc(sizeof(struct obd_client_handle), GFP_NOFS); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 664 | if (!*och_p) { |
| 665 | rc = -ENOMEM; |
| 666 | goto out_och_free; |
| 667 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 668 | |
| 669 | (*och_usecount)++; |
| 670 | |
| 671 | /* md_intent_lock() didn't get a request ref if there was an |
| 672 | * open error, so don't do cleanup on the request here |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 673 | * (bug 3430) |
| 674 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 675 | /* XXX (green): Should not we bail out on any error here, not |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 676 | * just open error? |
| 677 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 678 | rc = it_open_error(DISP_OPEN_OPEN, it); |
| 679 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 680 | goto out_och_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 681 | |
Andreas Dilger | 5787be9 | 2016-04-27 21:37:17 -0400 | [diff] [blame] | 682 | LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF), |
| 683 | "inode %p: disposition %x, status %d\n", inode, |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 684 | it_disposition(it, ~0), it->it_status); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 685 | |
| 686 | rc = ll_local_open(file, it, fd, *och_p); |
| 687 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 688 | goto out_och_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 689 | } |
| 690 | mutex_unlock(&lli->lli_och_mutex); |
| 691 | fd = NULL; |
| 692 | |
| 693 | /* Must do this outside lli_och_mutex lock to prevent deadlock where |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 694 | * different kind of OPEN lock for this same inode gets cancelled |
| 695 | * by ldlm_cancel_lru |
| 696 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 697 | if (!S_ISREG(inode->i_mode)) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 698 | goto out_och_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 699 | |
Andreas Dilger | 38585cc | 2014-02-11 02:52:05 -0700 | [diff] [blame] | 700 | if (!lli->lli_has_smd && |
| 701 | (cl_is_lov_delay_create(file->f_flags) || |
| 702 | (file->f_mode & FMODE_WRITE) == 0)) { |
| 703 | CDEBUG(D_INODE, "object creation was delayed\n"); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 704 | goto out_och_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 705 | } |
Andreas Dilger | 38585cc | 2014-02-11 02:52:05 -0700 | [diff] [blame] | 706 | cl_lov_delay_create_clear(&file->f_flags); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 707 | goto out_och_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 708 | |
| 709 | out_och_free: |
| 710 | if (rc) { |
| 711 | if (och_p && *och_p) { |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 712 | kfree(*och_p); |
Oleg Drokin | c0a2472 | 2015-09-16 12:26:58 -0400 | [diff] [blame] | 713 | *och_p = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 714 | (*och_usecount)--; |
| 715 | } |
| 716 | mutex_unlock(&lli->lli_och_mutex); |
| 717 | |
| 718 | out_openerr: |
| 719 | if (opendir_set != 0) |
| 720 | ll_stop_statahead(inode, lli->lli_opendir_key); |
Markus Elfring | a5cb888 | 2015-07-14 09:35:42 +0200 | [diff] [blame] | 721 | ll_file_data_put(fd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 722 | } else { |
| 723 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1); |
| 724 | } |
| 725 | |
| 726 | if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) { |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 727 | ptlrpc_req_finished(it->it_request); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 728 | it_clear_disposition(it, DISP_ENQ_OPEN_REF); |
| 729 | } |
| 730 | |
| 731 | return rc; |
| 732 | } |
| 733 | |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 734 | static int ll_md_blocking_lease_ast(struct ldlm_lock *lock, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 735 | struct ldlm_lock_desc *desc, |
| 736 | void *data, int flag) |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 737 | { |
| 738 | int rc; |
| 739 | struct lustre_handle lockh; |
| 740 | |
| 741 | switch (flag) { |
| 742 | case LDLM_CB_BLOCKING: |
| 743 | ldlm_lock2handle(lock, &lockh); |
| 744 | rc = ldlm_cli_cancel(&lockh, LCF_ASYNC); |
| 745 | if (rc < 0) { |
| 746 | CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc); |
| 747 | return rc; |
| 748 | } |
| 749 | break; |
| 750 | case LDLM_CB_CANCELING: |
| 751 | /* do nothing */ |
| 752 | break; |
| 753 | } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Acquire a lease and open the file. |
| 759 | */ |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 760 | static struct obd_client_handle * |
| 761 | ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode, |
| 762 | __u64 open_flags) |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 763 | { |
| 764 | struct lookup_intent it = { .it_op = IT_OPEN }; |
| 765 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 766 | struct md_op_data *op_data; |
| 767 | struct ptlrpc_request *req; |
| 768 | struct lustre_handle old_handle = { 0 }; |
| 769 | struct obd_client_handle *och = NULL; |
| 770 | int rc; |
| 771 | int rc2; |
| 772 | |
| 773 | if (fmode != FMODE_WRITE && fmode != FMODE_READ) |
| 774 | return ERR_PTR(-EINVAL); |
| 775 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 776 | if (file) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 777 | struct ll_inode_info *lli = ll_i2info(inode); |
| 778 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 779 | struct obd_client_handle **och_p; |
| 780 | __u64 *och_usecount; |
| 781 | |
| 782 | if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC)) |
| 783 | return ERR_PTR(-EPERM); |
| 784 | |
| 785 | /* Get the openhandle of the file */ |
| 786 | rc = -EBUSY; |
| 787 | mutex_lock(&lli->lli_och_mutex); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 788 | if (fd->fd_lease_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 789 | mutex_unlock(&lli->lli_och_mutex); |
| 790 | return ERR_PTR(rc); |
| 791 | } |
| 792 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 793 | if (!fd->fd_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 794 | if (file->f_mode & FMODE_WRITE) { |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 795 | LASSERT(lli->lli_mds_write_och); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 796 | och_p = &lli->lli_mds_write_och; |
| 797 | och_usecount = &lli->lli_open_fd_write_count; |
| 798 | } else { |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 799 | LASSERT(lli->lli_mds_read_och); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 800 | och_p = &lli->lli_mds_read_och; |
| 801 | och_usecount = &lli->lli_open_fd_read_count; |
| 802 | } |
| 803 | if (*och_usecount == 1) { |
| 804 | fd->fd_och = *och_p; |
| 805 | *och_p = NULL; |
| 806 | *och_usecount = 0; |
| 807 | rc = 0; |
| 808 | } |
| 809 | } |
| 810 | mutex_unlock(&lli->lli_och_mutex); |
| 811 | if (rc < 0) /* more than 1 opener */ |
| 812 | return ERR_PTR(rc); |
| 813 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 814 | LASSERT(fd->fd_och); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 815 | old_handle = fd->fd_och->och_fh; |
| 816 | } |
| 817 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 818 | och = kzalloc(sizeof(*och), GFP_NOFS); |
| 819 | if (!och) |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 820 | return ERR_PTR(-ENOMEM); |
| 821 | |
| 822 | op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 823 | LUSTRE_OPC_ANY, NULL); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 824 | if (IS_ERR(op_data)) { |
| 825 | rc = PTR_ERR(op_data); |
| 826 | goto out; |
| 827 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 828 | |
| 829 | /* To tell the MDT this openhandle is from the same owner */ |
| 830 | op_data->op_handle = old_handle; |
| 831 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 832 | it.it_flags = fmode | open_flags; |
| 833 | it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 834 | rc = md_intent_lock(sbi->ll_md_exp, op_data, NULL, 0, &it, 0, &req, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 835 | ll_md_blocking_lease_ast, |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 836 | /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise |
| 837 | * it can be cancelled which may mislead applications that the lease is |
| 838 | * broken; |
| 839 | * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal |
| 840 | * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 841 | * doesn't deal with openhandle, so normal openhandle will be leaked. |
| 842 | */ |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 843 | LDLM_FL_NO_LRU | LDLM_FL_EXCL); |
| 844 | ll_finish_md_op_data(op_data); |
Lai Siyao | f236f69 | 2014-02-28 21:16:38 -0500 | [diff] [blame] | 845 | ptlrpc_req_finished(req); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 846 | if (rc < 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 847 | goto out_release_it; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 848 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 849 | if (it_disposition(&it, DISP_LOOKUP_NEG)) { |
| 850 | rc = -ENOENT; |
| 851 | goto out_release_it; |
| 852 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 853 | |
| 854 | rc = it_open_error(DISP_OPEN_OPEN, &it); |
| 855 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 856 | goto out_release_it; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 857 | |
| 858 | LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF)); |
| 859 | ll_och_fill(sbi->ll_md_exp, &it, och); |
| 860 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 861 | if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */ { |
| 862 | rc = -EOPNOTSUPP; |
| 863 | goto out_close; |
| 864 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 865 | |
| 866 | /* already get lease, handle lease lock */ |
| 867 | ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL); |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 868 | if (it.it_lock_mode == 0 || |
| 869 | it.it_lock_bits != MDS_INODELOCK_OPEN) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 870 | /* open lock must return for lease */ |
| 871 | CERROR(DFID "lease granted but no open lock, %d/%llu.\n", |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 872 | PFID(ll_inode2fid(inode)), it.it_lock_mode, |
| 873 | it.it_lock_bits); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 874 | rc = -EPROTO; |
| 875 | goto out_close; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | ll_intent_release(&it); |
| 879 | return och; |
| 880 | |
| 881 | out_close: |
Jinshan Xiong | e55a68b | 2016-04-04 21:37:00 -0400 | [diff] [blame] | 882 | /* Cancel open lock */ |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 883 | if (it.it_lock_mode != 0) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 884 | ldlm_lock_decref_and_cancel(&och->och_lease_handle, |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 885 | it.it_lock_mode); |
| 886 | it.it_lock_mode = 0; |
Jinshan Xiong | e55a68b | 2016-04-04 21:37:00 -0400 | [diff] [blame] | 887 | och->och_lease_handle.cookie = 0ULL; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 888 | } |
Jinshan Xiong | e55a68b | 2016-04-04 21:37:00 -0400 | [diff] [blame] | 889 | rc2 = ll_close_inode_openhandle(sbi->ll_md_exp, inode, och, NULL); |
| 890 | if (rc2 < 0) |
| 891 | CERROR("%s: error closing file "DFID": %d\n", |
| 892 | ll_get_fsname(inode->i_sb, NULL, 0), |
| 893 | PFID(&ll_i2info(inode)->lli_fid), rc2); |
| 894 | och = NULL; /* och has been freed in ll_close_inode_openhandle() */ |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 895 | out_release_it: |
| 896 | ll_intent_release(&it); |
| 897 | out: |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 898 | kfree(och); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 899 | return ERR_PTR(rc); |
| 900 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 901 | |
| 902 | /** |
| 903 | * Release lease and close the file. |
| 904 | * It will check if the lease has ever broken. |
| 905 | */ |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 906 | static int ll_lease_close(struct obd_client_handle *och, struct inode *inode, |
| 907 | bool *lease_broken) |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 908 | { |
| 909 | struct ldlm_lock *lock; |
| 910 | bool cancelled = true; |
| 911 | int rc; |
| 912 | |
| 913 | lock = ldlm_handle2lock(&och->och_lease_handle); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 914 | if (lock) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 915 | lock_res_and_lock(lock); |
| 916 | cancelled = ldlm_is_cancel(lock); |
| 917 | unlock_res_and_lock(lock); |
Jinshan Xiong | ead0280 | 2016-04-04 21:36:51 -0400 | [diff] [blame] | 918 | LDLM_LOCK_PUT(lock); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 919 | } |
| 920 | |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 921 | CDEBUG(D_INODE, "lease for " DFID " broken? %d\n", |
| 922 | PFID(&ll_i2info(inode)->lli_fid), cancelled); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 923 | |
| 924 | if (!cancelled) |
| 925 | ldlm_cli_cancel(&och->och_lease_handle, 0); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 926 | if (lease_broken) |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 927 | *lease_broken = cancelled; |
| 928 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 929 | rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och, |
| 930 | NULL); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 931 | return rc; |
| 932 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 933 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 934 | /* Fills the obdo with the attributes for the lsm */ |
| 935 | static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp, |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 936 | struct obdo *obdo, __u64 ioepoch, int dv_flags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 937 | { |
| 938 | struct ptlrpc_request_set *set; |
Arnd Bergmann | 45efd65 | 2015-09-27 16:45:03 -0400 | [diff] [blame] | 939 | struct obd_info oinfo = { }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 940 | int rc; |
| 941 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 942 | LASSERT(lsm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 943 | |
| 944 | oinfo.oi_md = lsm; |
| 945 | oinfo.oi_oa = obdo; |
| 946 | oinfo.oi_oa->o_oi = lsm->lsm_oi; |
| 947 | oinfo.oi_oa->o_mode = S_IFREG; |
| 948 | oinfo.oi_oa->o_ioepoch = ioepoch; |
| 949 | oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | |
| 950 | OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | |
| 951 | OBD_MD_FLBLKSZ | OBD_MD_FLATIME | |
| 952 | OBD_MD_FLMTIME | OBD_MD_FLCTIME | |
| 953 | OBD_MD_FLGROUP | OBD_MD_FLEPOCH | |
| 954 | OBD_MD_FLDATAVERSION; |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 955 | if (dv_flags & (LL_DV_WR_FLUSH | LL_DV_RD_FLUSH)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 956 | oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS; |
| 957 | oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK; |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 958 | if (dv_flags & LL_DV_WR_FLUSH) |
| 959 | oinfo.oi_oa->o_flags |= OBD_FL_FLUSH; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | set = ptlrpc_prep_set(); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 963 | if (!set) { |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 964 | CERROR("cannot allocate ptlrpc set: rc = %d\n", -ENOMEM); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 965 | rc = -ENOMEM; |
| 966 | } else { |
| 967 | rc = obd_getattr_async(exp, &oinfo, set); |
| 968 | if (rc == 0) |
| 969 | rc = ptlrpc_set_wait(set); |
| 970 | ptlrpc_set_destroy(set); |
| 971 | } |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 972 | if (rc == 0) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 973 | oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | |
| 974 | OBD_MD_FLATIME | OBD_MD_FLMTIME | |
| 975 | OBD_MD_FLCTIME | OBD_MD_FLSIZE | |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 976 | OBD_MD_FLDATAVERSION | OBD_MD_FLFLAGS); |
| 977 | if (dv_flags & LL_DV_WR_FLUSH && |
| 978 | !(oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS && |
| 979 | oinfo.oi_oa->o_flags & OBD_FL_FLUSH)) |
| 980 | return -ENOTSUPP; |
| 981 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 982 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Performs the getattr on the inode and updates its fields. |
| 987 | * If @sync != 0, perform the getattr under the server-side lock. |
| 988 | */ |
| 989 | int ll_inode_getattr(struct inode *inode, struct obdo *obdo, |
| 990 | __u64 ioepoch, int sync) |
| 991 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 992 | struct lov_stripe_md *lsm; |
| 993 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 994 | |
| 995 | lsm = ccc_inode_lsm_get(inode); |
| 996 | rc = ll_lsm_getattr(lsm, ll_i2dtexp(inode), |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 997 | obdo, ioepoch, sync ? LL_DV_RD_FLUSH : 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 998 | if (rc == 0) { |
| 999 | struct ost_id *oi = lsm ? &lsm->lsm_oi : &obdo->o_oi; |
| 1000 | |
| 1001 | obdo_refresh_inode(inode, obdo, obdo->o_valid); |
Joe Perches | 2d00bd1 | 2014-11-23 11:28:50 -0800 | [diff] [blame] | 1002 | CDEBUG(D_INODE, "objid " DOSTID " size %llu, blocks %llu, blksize %lu\n", |
| 1003 | POSTID(oi), i_size_read(inode), |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1004 | (unsigned long long)inode->i_blocks, |
John L. Hammond | 16e0631 | 2014-09-09 13:39:03 -0500 | [diff] [blame] | 1005 | 1UL << inode->i_blkbits); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1006 | } |
| 1007 | ccc_inode_lsm_put(inode, lsm); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1008 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1011 | int ll_merge_attr(const struct lu_env *env, struct inode *inode) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1012 | { |
| 1013 | struct ll_inode_info *lli = ll_i2info(inode); |
| 1014 | struct cl_object *obj = lli->lli_clob; |
John Hammond | 9acc450 | 2016-03-30 19:48:57 -0400 | [diff] [blame] | 1015 | struct cl_attr *attr = vvp_env_thread_attr(env); |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1016 | s64 atime; |
| 1017 | s64 mtime; |
| 1018 | s64 ctime; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1019 | int rc = 0; |
| 1020 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1021 | ll_inode_size_lock(inode); |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1022 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1023 | /* merge timestamps the most recently obtained from mds with |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1024 | * timestamps obtained from osts |
| 1025 | */ |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1026 | LTIME_S(inode->i_atime) = lli->lli_atime; |
| 1027 | LTIME_S(inode->i_mtime) = lli->lli_mtime; |
| 1028 | LTIME_S(inode->i_ctime) = lli->lli_ctime; |
John L. Hammond | 376ef86 | 2014-08-28 18:35:14 -0500 | [diff] [blame] | 1029 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1030 | mtime = LTIME_S(inode->i_mtime); |
| 1031 | atime = LTIME_S(inode->i_atime); |
| 1032 | ctime = LTIME_S(inode->i_ctime); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1033 | |
| 1034 | cl_object_attr_lock(obj); |
| 1035 | rc = cl_object_attr_get(env, obj, attr); |
| 1036 | cl_object_attr_unlock(obj); |
| 1037 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1038 | if (rc != 0) |
| 1039 | goto out_size_unlock; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1040 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1041 | if (atime < attr->cat_atime) |
| 1042 | atime = attr->cat_atime; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1043 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1044 | if (ctime < attr->cat_ctime) |
| 1045 | ctime = attr->cat_ctime; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1046 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1047 | if (mtime < attr->cat_mtime) |
| 1048 | mtime = attr->cat_mtime; |
| 1049 | |
| 1050 | CDEBUG(D_VFSTRACE, DFID " updating i_size %llu\n", |
| 1051 | PFID(&lli->lli_fid), attr->cat_size); |
| 1052 | |
John L. Hammond | 1929c43 | 2016-03-30 19:48:37 -0400 | [diff] [blame] | 1053 | i_size_write(inode, attr->cat_size); |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1054 | |
| 1055 | inode->i_blocks = attr->cat_blocks; |
| 1056 | |
| 1057 | LTIME_S(inode->i_mtime) = mtime; |
| 1058 | LTIME_S(inode->i_atime) = atime; |
| 1059 | LTIME_S(inode->i_ctime) = ctime; |
| 1060 | |
| 1061 | out_size_unlock: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1062 | ll_inode_size_unlock(inode); |
| 1063 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1064 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm, |
| 1068 | lstat_t *st) |
| 1069 | { |
| 1070 | struct obdo obdo = { 0 }; |
| 1071 | int rc; |
| 1072 | |
Oleg Drokin | ef2e0f5 | 2015-09-27 16:45:46 -0400 | [diff] [blame] | 1073 | rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, &obdo, 0, 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1074 | if (rc == 0) { |
| 1075 | st->st_size = obdo.o_size; |
| 1076 | st->st_blocks = obdo.o_blocks; |
| 1077 | st->st_mtime = obdo.o_mtime; |
| 1078 | st->st_atime = obdo.o_atime; |
| 1079 | st->st_ctime = obdo.o_ctime; |
| 1080 | } |
| 1081 | return rc; |
| 1082 | } |
| 1083 | |
John L. Hammond | ec9bca9 | 2014-02-28 21:16:35 -0500 | [diff] [blame] | 1084 | static bool file_is_noatime(const struct file *file) |
| 1085 | { |
| 1086 | const struct vfsmount *mnt = file->f_path.mnt; |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 1087 | const struct inode *inode = file_inode(file); |
John L. Hammond | ec9bca9 | 2014-02-28 21:16:35 -0500 | [diff] [blame] | 1088 | |
| 1089 | /* Adapted from file_accessed() and touch_atime().*/ |
| 1090 | if (file->f_flags & O_NOATIME) |
| 1091 | return true; |
| 1092 | |
| 1093 | if (inode->i_flags & S_NOATIME) |
| 1094 | return true; |
| 1095 | |
| 1096 | if (IS_NOATIME(inode)) |
| 1097 | return true; |
| 1098 | |
| 1099 | if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY)) |
| 1100 | return true; |
| 1101 | |
| 1102 | if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) |
| 1103 | return true; |
| 1104 | |
| 1105 | if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)) |
| 1106 | return true; |
| 1107 | |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1111 | void ll_io_init(struct cl_io *io, const struct file *file, int write) |
| 1112 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 1113 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1114 | |
| 1115 | io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK; |
| 1116 | if (write) { |
| 1117 | io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND); |
| 1118 | io->u.ci_wr.wr_sync = file->f_flags & O_SYNC || |
| 1119 | file->f_flags & O_DIRECT || |
| 1120 | IS_SYNC(inode); |
| 1121 | } |
| 1122 | io->ci_obj = ll_i2info(inode)->lli_clob; |
| 1123 | io->ci_lockreq = CILR_MAYBE; |
| 1124 | if (ll_file_nolock(file)) { |
| 1125 | io->ci_lockreq = CILR_NEVER; |
| 1126 | io->ci_no_srvlock = 1; |
| 1127 | } else if (file->f_flags & O_APPEND) { |
| 1128 | io->ci_lockreq = CILR_MANDATORY; |
| 1129 | } |
John L. Hammond | ec9bca9 | 2014-02-28 21:16:35 -0500 | [diff] [blame] | 1130 | |
| 1131 | io->ci_noatime = file_is_noatime(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static ssize_t |
| 1135 | ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args, |
| 1136 | struct file *file, enum cl_io_type iot, |
| 1137 | loff_t *ppos, size_t count) |
| 1138 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 1139 | struct ll_inode_info *lli = ll_i2info(file_inode(file)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1140 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 1141 | struct cl_io *io; |
| 1142 | ssize_t result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1143 | |
Jinshan Xiong | 77605e4 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 1144 | CDEBUG(D_VFSTRACE, "file: %s, type: %d ppos: %llu, count: %zd\n", |
| 1145 | file->f_path.dentry->d_name.name, iot, *ppos, count); |
| 1146 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1147 | restart: |
John Hammond | 9acc450 | 2016-03-30 19:48:57 -0400 | [diff] [blame] | 1148 | io = vvp_env_thread_io(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1149 | ll_io_init(io, file, iot == CIT_WRITE); |
| 1150 | |
| 1151 | if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) { |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1152 | struct vvp_io *vio = vvp_env_io(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1153 | int write_mutex_locked = 0; |
| 1154 | |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1155 | vio->vui_fd = LUSTRE_FPRIVATE(file); |
| 1156 | vio->vui_io_subtype = args->via_io_subtype; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1157 | |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1158 | switch (vio->vui_io_subtype) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1159 | case IO_NORMAL: |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1160 | vio->vui_iter = args->u.normal.via_iter; |
| 1161 | vio->vui_iocb = args->u.normal.via_iocb; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1162 | if ((iot == CIT_WRITE) && |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1163 | !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1164 | if (mutex_lock_interruptible(&lli-> |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1165 | lli_write_mutex)) { |
| 1166 | result = -ERESTARTSYS; |
| 1167 | goto out; |
| 1168 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1169 | write_mutex_locked = 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1170 | } |
Jinshan Xiong | 77605e4 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 1171 | down_read(&lli->lli_trunc_sem); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1172 | break; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1173 | case IO_SPLICE: |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1174 | vio->u.splice.vui_pipe = args->u.splice.via_pipe; |
| 1175 | vio->u.splice.vui_flags = args->u.splice.via_flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1176 | break; |
| 1177 | default: |
John L. Hammond | e0a8144 | 2016-03-30 19:48:52 -0400 | [diff] [blame] | 1178 | CERROR("Unknown IO type - %u\n", vio->vui_io_subtype); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1179 | LBUG(); |
| 1180 | } |
Jinshan Xiong | 966c4a8 | 2016-06-05 23:28:51 -0400 | [diff] [blame] | 1181 | ll_cl_add(file, env, io); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1182 | result = cl_io_loop(env, io); |
Jinshan Xiong | 966c4a8 | 2016-06-05 23:28:51 -0400 | [diff] [blame] | 1183 | ll_cl_remove(file, env); |
Jinshan Xiong | 77605e4 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 1184 | if (args->via_io_subtype == IO_NORMAL) |
| 1185 | up_read(&lli->lli_trunc_sem); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1186 | if (write_mutex_locked) |
| 1187 | mutex_unlock(&lli->lli_write_mutex); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1188 | } else { |
| 1189 | /* cl_io_rw_init() handled IO */ |
| 1190 | result = io->ci_result; |
| 1191 | } |
| 1192 | |
| 1193 | if (io->ci_nob > 0) { |
| 1194 | result = io->ci_nob; |
| 1195 | *ppos = io->u.ci_wr.wr.crw_pos; |
| 1196 | } |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1197 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1198 | out: |
| 1199 | cl_io_fini(env, io); |
| 1200 | /* If any bit been read/written (result != 0), we just return |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1201 | * short read/write instead of restart io. |
| 1202 | */ |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 1203 | if ((result == 0 || result == -ENODATA) && io->ci_need_restart) { |
Al Viro | 09561a5 | 2014-05-07 21:10:24 -0400 | [diff] [blame] | 1204 | CDEBUG(D_VFSTRACE, "Restart %s on %pD from %lld, count:%zd\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1205 | iot == CIT_READ ? "read" : "write", |
Al Viro | 09561a5 | 2014-05-07 21:10:24 -0400 | [diff] [blame] | 1206 | file, *ppos, count); |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 1207 | LASSERTF(io->ci_nob == 0, "%zd\n", io->ci_nob); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1208 | goto restart; |
| 1209 | } |
| 1210 | |
| 1211 | if (iot == CIT_READ) { |
| 1212 | if (result >= 0) |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 1213 | ll_stats_ops_tally(ll_i2sbi(file_inode(file)), |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1214 | LPROC_LL_READ_BYTES, result); |
| 1215 | } else if (iot == CIT_WRITE) { |
| 1216 | if (result >= 0) { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 1217 | ll_stats_ops_tally(ll_i2sbi(file_inode(file)), |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1218 | LPROC_LL_WRITE_BYTES, result); |
| 1219 | fd->fd_write_failed = false; |
| 1220 | } else if (result != -ERESTARTSYS) { |
| 1221 | fd->fd_write_failed = true; |
| 1222 | } |
| 1223 | } |
Jinshan Xiong | 77605e4 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 1224 | CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1225 | |
| 1226 | return result; |
| 1227 | } |
| 1228 | |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 1229 | static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1230 | { |
| 1231 | struct lu_env *env; |
| 1232 | struct vvp_io_args *args; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1233 | ssize_t result; |
| 1234 | int refcheck; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1235 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1236 | env = cl_env_get(&refcheck); |
| 1237 | if (IS_ERR(env)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1238 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1239 | |
John Hammond | 9989a58 | 2016-03-30 19:48:56 -0400 | [diff] [blame] | 1240 | args = ll_env_args(env, IO_NORMAL); |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 1241 | args->u.normal.via_iter = to; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1242 | args->u.normal.via_iocb = iocb; |
| 1243 | |
| 1244 | result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ, |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 1245 | &iocb->ki_pos, iov_iter_count(to)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1246 | cl_env_put(env, &refcheck); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1247 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Write to a file (through the page cache). |
| 1252 | */ |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 1253 | static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1254 | { |
| 1255 | struct lu_env *env; |
| 1256 | struct vvp_io_args *args; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1257 | ssize_t result; |
| 1258 | int refcheck; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1259 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1260 | env = cl_env_get(&refcheck); |
| 1261 | if (IS_ERR(env)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1262 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1263 | |
John Hammond | 9989a58 | 2016-03-30 19:48:56 -0400 | [diff] [blame] | 1264 | args = ll_env_args(env, IO_NORMAL); |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 1265 | args->u.normal.via_iter = from; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1266 | args->u.normal.via_iocb = iocb; |
| 1267 | |
| 1268 | result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1269 | &iocb->ki_pos, iov_iter_count(from)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1270 | cl_env_put(env, &refcheck); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1271 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1274 | /* |
| 1275 | * Send file content (through pagecache) somewhere with helper |
| 1276 | */ |
| 1277 | static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos, |
| 1278 | struct pipe_inode_info *pipe, size_t count, |
| 1279 | unsigned int flags) |
| 1280 | { |
| 1281 | struct lu_env *env; |
| 1282 | struct vvp_io_args *args; |
| 1283 | ssize_t result; |
| 1284 | int refcheck; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1285 | |
| 1286 | env = cl_env_get(&refcheck); |
| 1287 | if (IS_ERR(env)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1288 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1289 | |
John Hammond | 9989a58 | 2016-03-30 19:48:56 -0400 | [diff] [blame] | 1290 | args = ll_env_args(env, IO_SPLICE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1291 | args->u.splice.via_pipe = pipe; |
| 1292 | args->u.splice.via_flags = flags; |
| 1293 | |
| 1294 | result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count); |
| 1295 | cl_env_put(env, &refcheck); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1296 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 1299 | static int ll_lov_recreate(struct inode *inode, struct ost_id *oi, u32 ost_idx) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1300 | { |
| 1301 | struct obd_export *exp = ll_i2dtexp(inode); |
| 1302 | struct obd_trans_info oti = { 0 }; |
| 1303 | struct obdo *oa = NULL; |
| 1304 | int lsm_size; |
| 1305 | int rc = 0; |
| 1306 | struct lov_stripe_md *lsm = NULL, *lsm2; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1307 | |
Amitoj Kaur Chawla | 21068c4 | 2016-02-26 14:25:09 +0530 | [diff] [blame] | 1308 | oa = kmem_cache_zalloc(obdo_cachep, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1309 | if (!oa) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1310 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1311 | |
| 1312 | lsm = ccc_inode_lsm_get(inode); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1313 | if (!lsm_has_objects(lsm)) { |
| 1314 | rc = -ENOENT; |
| 1315 | goto out; |
| 1316 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1317 | |
| 1318 | lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) * |
| 1319 | (lsm->lsm_stripe_count)); |
| 1320 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1321 | lsm2 = libcfs_kvzalloc(lsm_size, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1322 | if (!lsm2) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1323 | rc = -ENOMEM; |
| 1324 | goto out; |
| 1325 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1326 | |
| 1327 | oa->o_oi = *oi; |
| 1328 | oa->o_nlink = ost_idx; |
| 1329 | oa->o_flags |= OBD_FL_RECREATE_OBJS; |
| 1330 | oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP; |
| 1331 | obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME | |
| 1332 | OBD_MD_FLMTIME | OBD_MD_FLCTIME); |
| 1333 | obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid); |
| 1334 | memcpy(lsm2, lsm, lsm_size); |
| 1335 | ll_inode_size_lock(inode); |
| 1336 | rc = obd_create(NULL, exp, oa, &lsm2, &oti); |
| 1337 | ll_inode_size_unlock(inode); |
| 1338 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1339 | kvfree(lsm2); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1340 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1341 | out: |
| 1342 | ccc_inode_lsm_put(inode, lsm); |
Mike Rapoport | 2ba262f | 2015-10-20 12:39:46 +0300 | [diff] [blame] | 1343 | kmem_cache_free(obdo_cachep, oa); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1344 | return rc; |
| 1345 | } |
| 1346 | |
| 1347 | static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg) |
| 1348 | { |
| 1349 | struct ll_recreate_obj ucreat; |
| 1350 | struct ost_id oi; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1351 | |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 1352 | if (!capable(CFS_CAP_SYS_ADMIN)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1353 | return -EPERM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1354 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 1355 | if (copy_from_user(&ucreat, (struct ll_recreate_obj __user *)arg, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1356 | sizeof(ucreat))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1357 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1358 | |
| 1359 | ostid_set_seq_mdt0(&oi); |
| 1360 | ostid_set_id(&oi, ucreat.lrc_id); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1361 | return ll_lov_recreate(inode, &oi, ucreat.lrc_ost_idx); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg) |
| 1365 | { |
| 1366 | struct lu_fid fid; |
| 1367 | struct ost_id oi; |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 1368 | u32 ost_idx; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1369 | |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 1370 | if (!capable(CFS_CAP_SYS_ADMIN)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1371 | return -EPERM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1372 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 1373 | if (copy_from_user(&fid, (struct lu_fid __user *)arg, sizeof(fid))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1374 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1375 | |
| 1376 | fid_to_ostid(&fid, &oi); |
| 1377 | ost_idx = (fid_seq(&fid) >> 16) & 0xffff; |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1378 | return ll_lov_recreate(inode, &oi, ost_idx); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1379 | } |
| 1380 | |
Al Viro | c139f3c | 2014-05-09 10:05:18 -0400 | [diff] [blame] | 1381 | int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry, |
Niu Yawei | d467220 | 2016-04-04 21:36:47 -0400 | [diff] [blame] | 1382 | __u64 flags, struct lov_user_md *lum, |
| 1383 | int lum_size) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1384 | { |
| 1385 | struct lov_stripe_md *lsm = NULL; |
| 1386 | struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags}; |
| 1387 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1388 | |
| 1389 | lsm = ccc_inode_lsm_get(inode); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1390 | if (lsm) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1391 | ccc_inode_lsm_put(inode, lsm); |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 1392 | CDEBUG(D_IOCTL, "stripe already exists for inode "DFID"\n", |
| 1393 | PFID(ll_inode2fid(inode))); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1394 | rc = -EEXIST; |
| 1395 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | ll_inode_size_lock(inode); |
Al Viro | c139f3c | 2014-05-09 10:05:18 -0400 | [diff] [blame] | 1399 | rc = ll_intent_file_open(dentry, lum, lum_size, &oit); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1400 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1401 | goto out_unlock; |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 1402 | rc = oit.it_status; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1403 | if (rc < 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1404 | goto out_req_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1405 | |
Al Viro | e22fdcc | 2014-10-20 20:39:57 -0400 | [diff] [blame] | 1406 | ll_release_openhandle(inode, &oit); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1407 | |
Andreas Dilger | 38585cc | 2014-02-11 02:52:05 -0700 | [diff] [blame] | 1408 | out_unlock: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1409 | ll_inode_size_unlock(inode); |
| 1410 | ll_intent_release(&oit); |
| 1411 | ccc_inode_lsm_put(inode, lsm); |
Andreas Dilger | 38585cc | 2014-02-11 02:52:05 -0700 | [diff] [blame] | 1412 | out: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1413 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1414 | out_req_free: |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 1415 | ptlrpc_req_finished((struct ptlrpc_request *)oit.it_request); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1416 | goto out; |
| 1417 | } |
| 1418 | |
| 1419 | int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename, |
| 1420 | struct lov_mds_md **lmmp, int *lmm_size, |
| 1421 | struct ptlrpc_request **request) |
| 1422 | { |
| 1423 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 1424 | struct mdt_body *body; |
| 1425 | struct lov_mds_md *lmm = NULL; |
| 1426 | struct ptlrpc_request *req = NULL; |
| 1427 | struct md_op_data *op_data; |
| 1428 | int rc, lmmsize; |
| 1429 | |
Brian Behlendorf | 4477934 | 2014-04-27 13:06:47 -0400 | [diff] [blame] | 1430 | rc = ll_get_default_mdsize(sbi, &lmmsize); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1431 | if (rc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1432 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1433 | |
| 1434 | op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, |
| 1435 | strlen(filename), lmmsize, |
| 1436 | LUSTRE_OPC_ANY, NULL); |
| 1437 | if (IS_ERR(op_data)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1438 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1439 | |
| 1440 | op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA; |
| 1441 | rc = md_getattr_name(sbi->ll_md_exp, op_data, &req); |
| 1442 | ll_finish_md_op_data(op_data); |
| 1443 | if (rc < 0) { |
Joe Perches | 2d00bd1 | 2014-11-23 11:28:50 -0800 | [diff] [blame] | 1444 | CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n", |
| 1445 | filename, rc); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1446 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1450 | |
| 1451 | lmmsize = body->eadatasize; |
| 1452 | |
| 1453 | if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) || |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1454 | lmmsize == 0) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1455 | rc = -ENODATA; |
| 1456 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1460 | |
| 1461 | if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) && |
| 1462 | (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1463 | rc = -EPROTO; |
| 1464 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * This is coming from the MDS, so is probably in |
| 1469 | * little endian. We convert it to host endian before |
| 1470 | * passing it to userspace. |
| 1471 | */ |
Julia Lawall | 1f6eaf8 | 2015-08-29 19:30:09 +0200 | [diff] [blame] | 1472 | if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) { |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1473 | int stripe_count; |
| 1474 | |
| 1475 | stripe_count = le16_to_cpu(lmm->lmm_stripe_count); |
| 1476 | if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED) |
| 1477 | stripe_count = 0; |
| 1478 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1479 | /* if function called for directory - we should |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1480 | * avoid swab not existent lsm objects |
| 1481 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1482 | if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) { |
| 1483 | lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm); |
| 1484 | if (S_ISREG(body->mode)) |
| 1485 | lustre_swab_lov_user_md_objects( |
| 1486 | ((struct lov_user_md_v1 *)lmm)->lmm_objects, |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1487 | stripe_count); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1488 | } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) { |
| 1489 | lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm); |
| 1490 | if (S_ISREG(body->mode)) |
| 1491 | lustre_swab_lov_user_md_objects( |
| 1492 | ((struct lov_user_md_v3 *)lmm)->lmm_objects, |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1493 | stripe_count); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | out: |
| 1498 | *lmmp = lmm; |
| 1499 | *lmm_size = lmmsize; |
| 1500 | *request = req; |
| 1501 | return rc; |
| 1502 | } |
| 1503 | |
| 1504 | static int ll_lov_setea(struct inode *inode, struct file *file, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1505 | unsigned long arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1506 | { |
Niu Yawei | d467220 | 2016-04-04 21:36:47 -0400 | [diff] [blame] | 1507 | __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1508 | struct lov_user_md *lump; |
| 1509 | int lum_size = sizeof(struct lov_user_md) + |
| 1510 | sizeof(struct lov_user_ost_data); |
| 1511 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1512 | |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 1513 | if (!capable(CFS_CAP_SYS_ADMIN)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1514 | return -EPERM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1515 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1516 | lump = libcfs_kvzalloc(lum_size, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1517 | if (!lump) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1518 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1519 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 1520 | if (copy_from_user(lump, (struct lov_user_md __user *)arg, lum_size)) { |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1521 | kvfree(lump); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1522 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1523 | } |
| 1524 | |
Al Viro | c139f3c | 2014-05-09 10:05:18 -0400 | [diff] [blame] | 1525 | rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, lump, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1526 | lum_size); |
Al Viro | c139f3c | 2014-05-09 10:05:18 -0400 | [diff] [blame] | 1527 | cl_lov_delay_create_clear(&file->f_flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1528 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1529 | kvfree(lump); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1530 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static int ll_lov_setstripe(struct inode *inode, struct file *file, |
| 1534 | unsigned long arg) |
| 1535 | { |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 1536 | struct lov_user_md_v3 lumv3; |
| 1537 | struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3; |
| 1538 | struct lov_user_md_v1 __user *lumv1p = (void __user *)arg; |
| 1539 | struct lov_user_md_v3 __user *lumv3p = (void __user *)arg; |
| 1540 | int lum_size, rc; |
Niu Yawei | d467220 | 2016-04-04 21:36:47 -0400 | [diff] [blame] | 1541 | __u64 flags = FMODE_WRITE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1542 | |
| 1543 | /* first try with v1 which is smaller than v3 */ |
| 1544 | lum_size = sizeof(struct lov_user_md_v1); |
| 1545 | if (copy_from_user(lumv1, lumv1p, lum_size)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1546 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1547 | |
| 1548 | if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) { |
| 1549 | lum_size = sizeof(struct lov_user_md_v3); |
| 1550 | if (copy_from_user(&lumv3, lumv3p, lum_size)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1551 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1552 | } |
| 1553 | |
Al Viro | c139f3c | 2014-05-09 10:05:18 -0400 | [diff] [blame] | 1554 | rc = ll_lov_setstripe_ea_info(inode, file->f_path.dentry, flags, lumv1, |
| 1555 | lum_size); |
| 1556 | cl_lov_delay_create_clear(&file->f_flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1557 | if (rc == 0) { |
| 1558 | struct lov_stripe_md *lsm; |
| 1559 | __u32 gen; |
| 1560 | |
| 1561 | put_user(0, &lumv1p->lmm_stripe_count); |
| 1562 | |
| 1563 | ll_layout_refresh(inode, &gen); |
| 1564 | lsm = ccc_inode_lsm_get(inode); |
| 1565 | rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), |
Oleg Drokin | e09bee3 | 2016-01-03 12:05:43 -0500 | [diff] [blame] | 1566 | 0, lsm, (void __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1567 | ccc_inode_lsm_put(inode, lsm); |
| 1568 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1569 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static int ll_lov_getstripe(struct inode *inode, unsigned long arg) |
| 1573 | { |
| 1574 | struct lov_stripe_md *lsm; |
| 1575 | int rc = -ENODATA; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1576 | |
| 1577 | lsm = ccc_inode_lsm_get(inode); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1578 | if (lsm) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1579 | rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, |
Oleg Drokin | e09bee3 | 2016-01-03 12:05:43 -0500 | [diff] [blame] | 1580 | lsm, (void __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1581 | ccc_inode_lsm_put(inode, lsm); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1582 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1583 | } |
| 1584 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 1585 | static int |
| 1586 | ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1587 | { |
| 1588 | struct ll_inode_info *lli = ll_i2info(inode); |
| 1589 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1590 | struct ll_grouplock grouplock; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1591 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1592 | |
Patrick Farrell | 431b567 | 2015-02-01 21:52:18 -0500 | [diff] [blame] | 1593 | if (arg == 0) { |
| 1594 | CWARN("group id for group lock must not be 0\n"); |
| 1595 | return -EINVAL; |
| 1596 | } |
| 1597 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1598 | if (ll_file_nolock(file)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1599 | return -EOPNOTSUPP; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1600 | |
| 1601 | spin_lock(&lli->lli_lock); |
| 1602 | if (fd->fd_flags & LL_FILE_GROUP_LOCKED) { |
| 1603 | CWARN("group lock already existed with gid %lu\n", |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1604 | fd->fd_grouplock.lg_gid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1605 | spin_unlock(&lli->lli_lock); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1606 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1607 | } |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1608 | LASSERT(!fd->fd_grouplock.lg_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1609 | spin_unlock(&lli->lli_lock); |
| 1610 | |
John L. Hammond | 1929c43 | 2016-03-30 19:48:37 -0400 | [diff] [blame] | 1611 | rc = cl_get_grouplock(ll_i2info(inode)->lli_clob, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1612 | arg, (file->f_flags & O_NONBLOCK), &grouplock); |
| 1613 | if (rc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1614 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1615 | |
| 1616 | spin_lock(&lli->lli_lock); |
| 1617 | if (fd->fd_flags & LL_FILE_GROUP_LOCKED) { |
| 1618 | spin_unlock(&lli->lli_lock); |
| 1619 | CERROR("another thread just won the race\n"); |
| 1620 | cl_put_grouplock(&grouplock); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1621 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | fd->fd_flags |= LL_FILE_GROUP_LOCKED; |
| 1625 | fd->fd_grouplock = grouplock; |
| 1626 | spin_unlock(&lli->lli_lock); |
| 1627 | |
| 1628 | CDEBUG(D_INFO, "group lock %lu obtained\n", arg); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1629 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Luca Ceresoli | 920b4f2 | 2015-01-18 15:06:50 +0100 | [diff] [blame] | 1632 | static int ll_put_grouplock(struct inode *inode, struct file *file, |
| 1633 | unsigned long arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1634 | { |
| 1635 | struct ll_inode_info *lli = ll_i2info(inode); |
| 1636 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1637 | struct ll_grouplock grouplock; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1638 | |
| 1639 | spin_lock(&lli->lli_lock); |
| 1640 | if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) { |
| 1641 | spin_unlock(&lli->lli_lock); |
| 1642 | CWARN("no group lock held\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1643 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1644 | } |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1645 | LASSERT(fd->fd_grouplock.lg_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1646 | |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1647 | if (fd->fd_grouplock.lg_gid != arg) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1648 | CWARN("group lock %lu doesn't match current id %lu\n", |
John Hammond | 98eae5e | 2016-03-30 19:48:55 -0400 | [diff] [blame] | 1649 | arg, fd->fd_grouplock.lg_gid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1650 | spin_unlock(&lli->lli_lock); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1651 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | grouplock = fd->fd_grouplock; |
| 1655 | memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock)); |
| 1656 | fd->fd_flags &= ~LL_FILE_GROUP_LOCKED; |
| 1657 | spin_unlock(&lli->lli_lock); |
| 1658 | |
| 1659 | cl_put_grouplock(&grouplock); |
| 1660 | CDEBUG(D_INFO, "group lock %lu released\n", arg); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1661 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * Close inode open handle |
| 1666 | * |
Al Viro | e22fdcc | 2014-10-20 20:39:57 -0400 | [diff] [blame] | 1667 | * \param inode [in] inode in question |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1668 | * \param it [in,out] intent which contains open info and result |
| 1669 | * |
| 1670 | * \retval 0 success |
| 1671 | * \retval <0 failure |
| 1672 | */ |
Al Viro | e22fdcc | 2014-10-20 20:39:57 -0400 | [diff] [blame] | 1673 | int ll_release_openhandle(struct inode *inode, struct lookup_intent *it) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1674 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1675 | struct obd_client_handle *och; |
| 1676 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1677 | |
| 1678 | LASSERT(inode); |
| 1679 | |
| 1680 | /* Root ? Do nothing. */ |
Al Viro | f76c23d | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 1681 | if (is_root_inode(inode)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1682 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1683 | |
| 1684 | /* No open handle to close? Move away */ |
| 1685 | if (!it_disposition(it, DISP_OPEN_OPEN)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1686 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1687 | |
| 1688 | LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0); |
| 1689 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 1690 | och = kzalloc(sizeof(*och), GFP_NOFS); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1691 | if (!och) { |
| 1692 | rc = -ENOMEM; |
| 1693 | goto out; |
| 1694 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1695 | |
John L. Hammond | ea1db08 | 2013-11-15 00:13:08 +0800 | [diff] [blame] | 1696 | ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1697 | |
| 1698 | rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1699 | inode, och, NULL); |
| 1700 | out: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1701 | /* this one is in place of ll_file_open */ |
| 1702 | if (it_disposition(it, DISP_ENQ_OPEN_REF)) { |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 1703 | ptlrpc_req_finished(it->it_request); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1704 | it_clear_disposition(it, DISP_ENQ_OPEN_REF); |
| 1705 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1706 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * Get size for inode for which FIEMAP mapping is requested. |
| 1711 | * Make the FIEMAP get_info call and returns the result. |
| 1712 | */ |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 1713 | static int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap, |
Bobi Jam | ebdc4fc | 2014-04-27 13:07:11 -0400 | [diff] [blame] | 1714 | size_t num_bytes) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1715 | { |
| 1716 | struct obd_export *exp = ll_i2dtexp(inode); |
| 1717 | struct lov_stripe_md *lsm = NULL; |
| 1718 | struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, }; |
Bobi Jam | ebdc4fc | 2014-04-27 13:07:11 -0400 | [diff] [blame] | 1719 | __u32 vallen = num_bytes; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1720 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1721 | |
| 1722 | /* Checks for fiemap flags */ |
| 1723 | if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) { |
| 1724 | fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT; |
| 1725 | return -EBADR; |
| 1726 | } |
| 1727 | |
| 1728 | /* Check for FIEMAP_FLAG_SYNC */ |
| 1729 | if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) { |
| 1730 | rc = filemap_fdatawrite(inode->i_mapping); |
| 1731 | if (rc) |
| 1732 | return rc; |
| 1733 | } |
| 1734 | |
| 1735 | lsm = ccc_inode_lsm_get(inode); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1736 | if (!lsm) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1737 | return -ENOENT; |
| 1738 | |
| 1739 | /* If the stripe_count > 1 and the application does not understand |
| 1740 | * DEVICE_ORDER flag, then it cannot interpret the extents correctly. |
| 1741 | */ |
| 1742 | if (lsm->lsm_stripe_count > 1 && |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1743 | !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) { |
| 1744 | rc = -EOPNOTSUPP; |
| 1745 | goto out; |
| 1746 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1747 | |
| 1748 | fm_key.oa.o_oi = lsm->lsm_oi; |
| 1749 | fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP; |
| 1750 | |
Li Dongyang | a915ffd | 2015-03-25 21:53:24 -0400 | [diff] [blame] | 1751 | if (i_size_read(inode) == 0) { |
| 1752 | rc = ll_glimpse_size(inode); |
| 1753 | if (rc) |
| 1754 | goto out; |
| 1755 | } |
| 1756 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1757 | obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE); |
| 1758 | obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid); |
| 1759 | /* If filesize is 0, then there would be no objects for mapping */ |
| 1760 | if (fm_key.oa.o_size == 0) { |
| 1761 | fiemap->fm_mapped_extents = 0; |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1762 | rc = 0; |
| 1763 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap)); |
| 1767 | |
| 1768 | rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen, |
| 1769 | fiemap, lsm); |
| 1770 | if (rc) |
| 1771 | CERROR("obd_get_info failed: rc = %d\n", rc); |
| 1772 | |
| 1773 | out: |
| 1774 | ccc_inode_lsm_put(inode, lsm); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1775 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1778 | int ll_fid2path(struct inode *inode, void __user *arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1779 | { |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1780 | struct obd_export *exp = ll_i2mdexp(inode); |
| 1781 | const struct getinfo_fid2path __user *gfin = arg; |
| 1782 | struct getinfo_fid2path *gfout; |
| 1783 | u32 pathlen; |
| 1784 | size_t outsize; |
| 1785 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1786 | |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 1787 | if (!capable(CFS_CAP_DAC_READ_SEARCH) && |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1788 | !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1789 | return -EPERM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1790 | |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1791 | /* Only need to get the buflen */ |
| 1792 | if (get_user(pathlen, &gfin->gf_pathlen)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1793 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1794 | |
Oleg Drokin | c7b09ef | 2014-08-15 12:48:13 -0400 | [diff] [blame] | 1795 | if (pathlen > PATH_MAX) |
| 1796 | return -EINVAL; |
| 1797 | |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1798 | outsize = sizeof(*gfout) + pathlen; |
| 1799 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 1800 | gfout = kzalloc(outsize, GFP_NOFS); |
| 1801 | if (!gfout) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1802 | return -ENOMEM; |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1803 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1804 | if (copy_from_user(gfout, arg, sizeof(*gfout))) { |
| 1805 | rc = -EFAULT; |
| 1806 | goto gf_free; |
| 1807 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1808 | |
| 1809 | /* Call mdc_iocontrol */ |
| 1810 | rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL); |
Frank Zago | 2b358b4 | 2014-08-15 12:48:12 -0400 | [diff] [blame] | 1811 | if (rc != 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1812 | goto gf_free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1813 | |
| 1814 | if (copy_to_user(arg, gfout, outsize)) |
| 1815 | rc = -EFAULT; |
| 1816 | |
| 1817 | gf_free: |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 1818 | kfree(gfout); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1819 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg) |
| 1823 | { |
| 1824 | struct ll_user_fiemap *fiemap_s; |
| 1825 | size_t num_bytes, ret_bytes; |
| 1826 | unsigned int extent_count; |
| 1827 | int rc = 0; |
| 1828 | |
| 1829 | /* Get the extent count so we can calculate the size of |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1830 | * required fiemap buffer |
| 1831 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1832 | if (get_user(extent_count, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1833 | &((struct ll_user_fiemap __user *)arg)->fm_extent_count)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1834 | return -EFAULT; |
Vitaly Osipov | 7bc3dfa | 2014-04-27 01:08:21 +1000 | [diff] [blame] | 1835 | |
| 1836 | if (extent_count >= |
| 1837 | (SIZE_MAX - sizeof(*fiemap_s)) / sizeof(struct ll_fiemap_extent)) |
| 1838 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1839 | num_bytes = sizeof(*fiemap_s) + (extent_count * |
| 1840 | sizeof(struct ll_fiemap_extent)); |
| 1841 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1842 | fiemap_s = libcfs_kvzalloc(num_bytes, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1843 | if (!fiemap_s) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1844 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1845 | |
| 1846 | /* get the fiemap value */ |
| 1847 | if (copy_from_user(fiemap_s, (struct ll_user_fiemap __user *)arg, |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1848 | sizeof(*fiemap_s))) { |
| 1849 | rc = -EFAULT; |
| 1850 | goto error; |
| 1851 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1852 | |
| 1853 | /* If fm_extent_count is non-zero, read the first extent since |
| 1854 | * it is used to calculate end_offset and device from previous |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1855 | * fiemap call. |
| 1856 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1857 | if (extent_count) { |
| 1858 | if (copy_from_user(&fiemap_s->fm_extents[0], |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 1859 | (char __user *)arg + sizeof(*fiemap_s), |
| 1860 | sizeof(struct ll_fiemap_extent))) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1861 | rc = -EFAULT; |
| 1862 | goto error; |
| 1863 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | rc = ll_do_fiemap(inode, fiemap_s, num_bytes); |
| 1867 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1868 | goto error; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1869 | |
| 1870 | ret_bytes = sizeof(struct ll_user_fiemap); |
| 1871 | |
| 1872 | if (extent_count != 0) |
| 1873 | ret_bytes += (fiemap_s->fm_mapped_extents * |
| 1874 | sizeof(struct ll_fiemap_extent)); |
| 1875 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 1876 | if (copy_to_user((void __user *)arg, fiemap_s, ret_bytes)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1877 | rc = -EFAULT; |
| 1878 | |
| 1879 | error: |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 1880 | kvfree(fiemap_s); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1881 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Read the data_version for inode. |
| 1886 | * |
| 1887 | * This value is computed using stripe object version on OST. |
| 1888 | * Version is computed using server side locking. |
| 1889 | * |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 1890 | * @param sync if do sync on the OST side; |
| 1891 | * 0: no sync |
| 1892 | * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs |
| 1893 | * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1894 | */ |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 1895 | int ll_data_version(struct inode *inode, __u64 *data_version, int flags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1896 | { |
| 1897 | struct lov_stripe_md *lsm = NULL; |
| 1898 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 1899 | struct obdo *obdo = NULL; |
| 1900 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1901 | |
| 1902 | /* If no stripe, we consider version is 0. */ |
| 1903 | lsm = ccc_inode_lsm_get(inode); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1904 | if (!lsm_has_objects(lsm)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1905 | *data_version = 0; |
| 1906 | CDEBUG(D_INODE, "No object for inode\n"); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1907 | rc = 0; |
| 1908 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 1911 | obdo = kzalloc(sizeof(*obdo), GFP_NOFS); |
| 1912 | if (!obdo) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1913 | rc = -ENOMEM; |
| 1914 | goto out; |
| 1915 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1916 | |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 1917 | rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, obdo, 0, flags); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1918 | if (rc == 0) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1919 | if (!(obdo->o_valid & OBD_MD_FLDATAVERSION)) |
| 1920 | rc = -EOPNOTSUPP; |
| 1921 | else |
| 1922 | *data_version = obdo->o_data_version; |
| 1923 | } |
| 1924 | |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 1925 | kfree(obdo); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1926 | out: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1927 | ccc_inode_lsm_put(inode, lsm); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1928 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1929 | } |
| 1930 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1931 | /* |
| 1932 | * Trigger a HSM release request for the provided inode. |
| 1933 | */ |
| 1934 | int ll_hsm_release(struct inode *inode) |
| 1935 | { |
| 1936 | struct cl_env_nest nest; |
| 1937 | struct lu_env *env; |
| 1938 | struct obd_client_handle *och = NULL; |
| 1939 | __u64 data_version = 0; |
| 1940 | int rc; |
| 1941 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1942 | CDEBUG(D_INODE, "%s: Releasing file "DFID".\n", |
| 1943 | ll_get_fsname(inode->i_sb, NULL, 0), |
| 1944 | PFID(&ll_i2info(inode)->lli_fid)); |
| 1945 | |
| 1946 | och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1947 | if (IS_ERR(och)) { |
| 1948 | rc = PTR_ERR(och); |
| 1949 | goto out; |
| 1950 | } |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1951 | |
| 1952 | /* Grab latest data_version and [am]time values */ |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 1953 | rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH); |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1954 | if (rc != 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1955 | goto out; |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1956 | |
| 1957 | env = cl_env_nested_get(&nest); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 1958 | if (IS_ERR(env)) { |
| 1959 | rc = PTR_ERR(env); |
| 1960 | goto out; |
| 1961 | } |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1962 | |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 1963 | ll_merge_attr(env, inode); |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1964 | cl_env_nested_put(&nest, env); |
| 1965 | |
| 1966 | /* Release the file. |
| 1967 | * NB: lease lock handle is released in mdc_hsm_release_pack() because |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 1968 | * we still need it to pack l_remote_handle to MDT. |
| 1969 | */ |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1970 | rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och, |
| 1971 | &data_version); |
| 1972 | och = NULL; |
| 1973 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1974 | out: |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 1975 | if (och && !IS_ERR(och)) /* close the file */ |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 1976 | ll_lease_close(och, inode, NULL); |
| 1977 | |
| 1978 | return rc; |
| 1979 | } |
| 1980 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1981 | struct ll_swap_stack { |
| 1982 | struct iattr ia1, ia2; |
| 1983 | __u64 dv1, dv2; |
| 1984 | struct inode *inode1, *inode2; |
| 1985 | bool check_dv1, check_dv2; |
| 1986 | }; |
| 1987 | |
| 1988 | static int ll_swap_layouts(struct file *file1, struct file *file2, |
| 1989 | struct lustre_swap_layouts *lsl) |
| 1990 | { |
| 1991 | struct mdc_swap_layouts msl; |
| 1992 | struct md_op_data *op_data; |
| 1993 | __u32 gid; |
| 1994 | __u64 dv; |
| 1995 | struct ll_swap_stack *llss = NULL; |
| 1996 | int rc; |
| 1997 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 1998 | llss = kzalloc(sizeof(*llss), GFP_NOFS); |
| 1999 | if (!llss) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2000 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2001 | |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2002 | llss->inode1 = file_inode(file1); |
| 2003 | llss->inode2 = file_inode(file2); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2004 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2005 | if (!S_ISREG(llss->inode2->i_mode)) { |
| 2006 | rc = -EINVAL; |
| 2007 | goto free; |
| 2008 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2009 | |
Greg Kroah-Hartman | 9c5fb72 | 2013-08-02 16:01:26 +0800 | [diff] [blame] | 2010 | if (inode_permission(llss->inode1, MAY_WRITE) || |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2011 | inode_permission(llss->inode2, MAY_WRITE)) { |
| 2012 | rc = -EPERM; |
| 2013 | goto free; |
| 2014 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2015 | |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2016 | if (llss->inode2->i_sb != llss->inode1->i_sb) { |
| 2017 | rc = -EXDEV; |
| 2018 | goto free; |
| 2019 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2020 | |
| 2021 | /* we use 2 bool because it is easier to swap than 2 bits */ |
| 2022 | if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1) |
| 2023 | llss->check_dv1 = true; |
| 2024 | |
| 2025 | if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2) |
| 2026 | llss->check_dv2 = true; |
| 2027 | |
| 2028 | /* we cannot use lsl->sl_dvX directly because we may swap them */ |
| 2029 | llss->dv1 = lsl->sl_dv1; |
| 2030 | llss->dv2 = lsl->sl_dv2; |
| 2031 | |
| 2032 | rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2)); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2033 | if (rc == 0) /* same file, done! */ { |
| 2034 | rc = 0; |
| 2035 | goto free; |
| 2036 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2037 | |
| 2038 | if (rc < 0) { /* sequentialize it */ |
| 2039 | swap(llss->inode1, llss->inode2); |
| 2040 | swap(file1, file2); |
| 2041 | swap(llss->dv1, llss->dv2); |
| 2042 | swap(llss->check_dv1, llss->check_dv2); |
| 2043 | } |
| 2044 | |
| 2045 | gid = lsl->sl_gid; |
| 2046 | if (gid != 0) { /* application asks to flush dirty cache */ |
| 2047 | rc = ll_get_grouplock(llss->inode1, file1, gid); |
| 2048 | if (rc < 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2049 | goto free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2050 | |
| 2051 | rc = ll_get_grouplock(llss->inode2, file2, gid); |
| 2052 | if (rc < 0) { |
| 2053 | ll_put_grouplock(llss->inode1, file1, gid); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2054 | goto free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | /* to be able to restore mtime and atime after swap |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2059 | * we need to first save them |
| 2060 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2061 | if (lsl->sl_flags & |
| 2062 | (SWAP_LAYOUTS_KEEP_MTIME | SWAP_LAYOUTS_KEEP_ATIME)) { |
| 2063 | llss->ia1.ia_mtime = llss->inode1->i_mtime; |
| 2064 | llss->ia1.ia_atime = llss->inode1->i_atime; |
| 2065 | llss->ia1.ia_valid = ATTR_MTIME | ATTR_ATIME; |
| 2066 | llss->ia2.ia_mtime = llss->inode2->i_mtime; |
| 2067 | llss->ia2.ia_atime = llss->inode2->i_atime; |
| 2068 | llss->ia2.ia_valid = ATTR_MTIME | ATTR_ATIME; |
| 2069 | } |
| 2070 | |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 2071 | /* ultimate check, before swapping the layouts we check if |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2072 | * dataversion has changed (if requested) |
| 2073 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2074 | if (llss->check_dv1) { |
| 2075 | rc = ll_data_version(llss->inode1, &dv, 0); |
| 2076 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2077 | goto putgl; |
| 2078 | if (dv != llss->dv1) { |
| 2079 | rc = -EAGAIN; |
| 2080 | goto putgl; |
| 2081 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | if (llss->check_dv2) { |
| 2085 | rc = ll_data_version(llss->inode2, &dv, 0); |
| 2086 | if (rc) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2087 | goto putgl; |
| 2088 | if (dv != llss->dv2) { |
| 2089 | rc = -EAGAIN; |
| 2090 | goto putgl; |
| 2091 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* struct md_op_data is used to send the swap args to the mdt |
| 2095 | * only flags is missing, so we use struct mdc_swap_layouts |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2096 | * through the md_op_data->op_data |
| 2097 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2098 | /* flags from user space have to be converted before they are send to |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2099 | * server, no flag is sent today, they are only used on the client |
| 2100 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2101 | msl.msl_flags = 0; |
| 2102 | rc = -ENOMEM; |
| 2103 | op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0, |
| 2104 | 0, LUSTRE_OPC_ANY, &msl); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2105 | if (IS_ERR(op_data)) { |
| 2106 | rc = PTR_ERR(op_data); |
| 2107 | goto free; |
| 2108 | } |
John L. Hammond | 79a8726 | 2013-07-23 00:06:29 +0800 | [diff] [blame] | 2109 | |
| 2110 | rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1), |
| 2111 | sizeof(*op_data), op_data, NULL); |
| 2112 | ll_finish_md_op_data(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2113 | |
| 2114 | putgl: |
| 2115 | if (gid != 0) { |
| 2116 | ll_put_grouplock(llss->inode2, file2, gid); |
| 2117 | ll_put_grouplock(llss->inode1, file1, gid); |
| 2118 | } |
| 2119 | |
| 2120 | /* rc can be set from obd_iocontrol() or from a GOTO(putgl, ...) */ |
| 2121 | if (rc != 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2122 | goto free; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2123 | |
| 2124 | /* clear useless flags */ |
| 2125 | if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_MTIME)) { |
| 2126 | llss->ia1.ia_valid &= ~ATTR_MTIME; |
| 2127 | llss->ia2.ia_valid &= ~ATTR_MTIME; |
| 2128 | } |
| 2129 | |
| 2130 | if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_ATIME)) { |
| 2131 | llss->ia1.ia_valid &= ~ATTR_ATIME; |
| 2132 | llss->ia2.ia_valid &= ~ATTR_ATIME; |
| 2133 | } |
| 2134 | |
| 2135 | /* update time if requested */ |
| 2136 | rc = 0; |
| 2137 | if (llss->ia2.ia_valid != 0) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2138 | inode_lock(llss->inode1); |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 2139 | rc = ll_setattr(file1->f_path.dentry, &llss->ia2); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2140 | inode_unlock(llss->inode1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | if (llss->ia1.ia_valid != 0) { |
| 2144 | int rc1; |
| 2145 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2146 | inode_lock(llss->inode2); |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 2147 | rc1 = ll_setattr(file2->f_path.dentry, &llss->ia1); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2148 | inode_unlock(llss->inode2); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2149 | if (rc == 0) |
| 2150 | rc = rc1; |
| 2151 | } |
| 2152 | |
| 2153 | free: |
Julia Lawall | e6b9a3b | 2015-05-01 21:38:01 +0200 | [diff] [blame] | 2154 | kfree(llss); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2155 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2156 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2157 | } |
| 2158 | |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2159 | static int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss) |
| 2160 | { |
| 2161 | struct md_op_data *op_data; |
| 2162 | int rc; |
| 2163 | |
Bruno Faccini | 2742c75 | 2015-09-14 18:41:20 -0400 | [diff] [blame] | 2164 | /* Detect out-of range masks */ |
| 2165 | if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK) |
| 2166 | return -EINVAL; |
| 2167 | |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2168 | /* Non-root users are forbidden to set or clear flags which are |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2169 | * NOT defined in HSM_USER_MASK. |
| 2170 | */ |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2171 | if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) && |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 2172 | !capable(CFS_CAP_SYS_ADMIN)) |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2173 | return -EPERM; |
| 2174 | |
Bruno Faccini | 2742c75 | 2015-09-14 18:41:20 -0400 | [diff] [blame] | 2175 | /* Detect out-of range archive id */ |
| 2176 | if ((hss->hss_valid & HSS_ARCHIVE_ID) && |
| 2177 | (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE)) |
| 2178 | return -EINVAL; |
| 2179 | |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2180 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0, |
| 2181 | LUSTRE_OPC_ANY, hss); |
| 2182 | if (IS_ERR(op_data)) |
| 2183 | return PTR_ERR(op_data); |
| 2184 | |
| 2185 | rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode), |
| 2186 | sizeof(*op_data), op_data, NULL); |
| 2187 | |
| 2188 | ll_finish_md_op_data(op_data); |
| 2189 | |
| 2190 | return rc; |
| 2191 | } |
| 2192 | |
| 2193 | static int ll_hsm_import(struct inode *inode, struct file *file, |
| 2194 | struct hsm_user_import *hui) |
| 2195 | { |
| 2196 | struct hsm_state_set *hss = NULL; |
| 2197 | struct iattr *attr = NULL; |
| 2198 | int rc; |
| 2199 | |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2200 | if (!S_ISREG(inode->i_mode)) |
| 2201 | return -EINVAL; |
| 2202 | |
| 2203 | /* set HSM flags */ |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 2204 | hss = kzalloc(sizeof(*hss), GFP_NOFS); |
Julia Lawall | e6b9a3b | 2015-05-01 21:38:01 +0200 | [diff] [blame] | 2205 | if (!hss) |
| 2206 | return -ENOMEM; |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2207 | |
| 2208 | hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID; |
| 2209 | hss->hss_archive_id = hui->hui_archive_id; |
| 2210 | hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED; |
| 2211 | rc = ll_hsm_state_set(inode, hss); |
| 2212 | if (rc != 0) |
Julia Lawall | e6b9a3b | 2015-05-01 21:38:01 +0200 | [diff] [blame] | 2213 | goto free_hss; |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2214 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 2215 | attr = kzalloc(sizeof(*attr), GFP_NOFS); |
| 2216 | if (!attr) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2217 | rc = -ENOMEM; |
Julia Lawall | e6b9a3b | 2015-05-01 21:38:01 +0200 | [diff] [blame] | 2218 | goto free_hss; |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2219 | } |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2220 | |
| 2221 | attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 2222 | attr->ia_mode |= S_IFREG; |
| 2223 | attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid); |
| 2224 | attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid); |
| 2225 | attr->ia_size = hui->hui_size; |
| 2226 | attr->ia_mtime.tv_sec = hui->hui_mtime; |
| 2227 | attr->ia_mtime.tv_nsec = hui->hui_mtime_ns; |
| 2228 | attr->ia_atime.tv_sec = hui->hui_atime; |
| 2229 | attr->ia_atime.tv_nsec = hui->hui_atime_ns; |
| 2230 | |
| 2231 | attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE | |
| 2232 | ATTR_UID | ATTR_GID | |
| 2233 | ATTR_MTIME | ATTR_MTIME_SET | |
| 2234 | ATTR_ATIME | ATTR_ATIME_SET; |
| 2235 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2236 | inode_lock(inode); |
John L. Hammond | b6ee56f | 2014-08-15 12:48:11 -0400 | [diff] [blame] | 2237 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 2238 | rc = ll_setattr_raw(file->f_path.dentry, attr, true); |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2239 | if (rc == -ENODATA) |
| 2240 | rc = 0; |
| 2241 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2242 | inode_unlock(inode); |
John L. Hammond | b6ee56f | 2014-08-15 12:48:11 -0400 | [diff] [blame] | 2243 | |
Julia Lawall | e6b9a3b | 2015-05-01 21:38:01 +0200 | [diff] [blame] | 2244 | kfree(attr); |
| 2245 | free_hss: |
| 2246 | kfree(hss); |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2247 | return rc; |
| 2248 | } |
| 2249 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2250 | static long |
| 2251 | ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2252 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2253 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2254 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 2255 | int flags, rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2256 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2257 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),cmd=%x\n", |
| 2258 | PFID(ll_inode2fid(inode)), inode, cmd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2259 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1); |
| 2260 | |
| 2261 | /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */ |
| 2262 | if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */ |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2263 | return -ENOTTY; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2264 | |
Greg Donald | a58a38a | 2014-08-21 12:40:35 -0500 | [diff] [blame] | 2265 | switch (cmd) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2266 | case LL_IOC_GETFLAGS: |
| 2267 | /* Get the current value of the file flags */ |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2268 | return put_user(fd->fd_flags, (int __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2269 | case LL_IOC_SETFLAGS: |
| 2270 | case LL_IOC_CLRFLAGS: |
| 2271 | /* Set or clear specific file flags */ |
| 2272 | /* XXX This probably needs checks to ensure the flags are |
| 2273 | * not abused, and to handle any flag side effects. |
| 2274 | */ |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2275 | if (get_user(flags, (int __user *)arg)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2276 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2277 | |
| 2278 | if (cmd == LL_IOC_SETFLAGS) { |
| 2279 | if ((flags & LL_FILE_IGNORE_LOCK) && |
| 2280 | !(file->f_flags & O_DIRECT)) { |
Joe Perches | 2d00bd1 | 2014-11-23 11:28:50 -0800 | [diff] [blame] | 2281 | CERROR("%s: unable to disable locking on non-O_DIRECT file\n", |
| 2282 | current->comm); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2283 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | fd->fd_flags |= flags; |
| 2287 | } else { |
| 2288 | fd->fd_flags &= ~flags; |
| 2289 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2290 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2291 | case LL_IOC_LOV_SETSTRIPE: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2292 | return ll_lov_setstripe(inode, file, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2293 | case LL_IOC_LOV_SETEA: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2294 | return ll_lov_setea(inode, file, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2295 | case LL_IOC_LOV_SWAP_LAYOUTS: { |
| 2296 | struct file *file2; |
| 2297 | struct lustre_swap_layouts lsl; |
| 2298 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2299 | if (copy_from_user(&lsl, (char __user *)arg, |
| 2300 | sizeof(struct lustre_swap_layouts))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2301 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2302 | |
| 2303 | if ((file->f_flags & O_ACCMODE) == 0) /* O_RDONLY */ |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2304 | return -EPERM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2305 | |
| 2306 | file2 = fget(lsl.sl_fd); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2307 | if (!file2) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2308 | return -EBADF; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2309 | |
| 2310 | rc = -EPERM; |
| 2311 | if ((file2->f_flags & O_ACCMODE) != 0) /* O_WRONLY or O_RDWR */ |
| 2312 | rc = ll_swap_layouts(file, file2, &lsl); |
| 2313 | fput(file2); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2314 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2315 | } |
| 2316 | case LL_IOC_LOV_GETSTRIPE: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2317 | return ll_lov_getstripe(inode, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2318 | case LL_IOC_RECREATE_OBJ: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2319 | return ll_lov_recreate_obj(inode, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2320 | case LL_IOC_RECREATE_FID: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2321 | return ll_lov_recreate_fid(inode, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2322 | case FSFILT_IOC_FIEMAP: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2323 | return ll_ioctl_fiemap(inode, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2324 | case FSFILT_IOC_GETFLAGS: |
| 2325 | case FSFILT_IOC_SETFLAGS: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2326 | return ll_iocontrol(inode, file, cmd, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2327 | case FSFILT_IOC_GETVERSION_OLD: |
| 2328 | case FSFILT_IOC_GETVERSION: |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2329 | return put_user(inode->i_generation, (int __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2330 | case LL_IOC_GROUP_LOCK: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2331 | return ll_get_grouplock(inode, file, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2332 | case LL_IOC_GROUP_UNLOCK: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2333 | return ll_put_grouplock(inode, file, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2334 | case IOC_OBD_STATFS: |
Oleg Drokin | 4c6243e | 2016-01-03 12:05:47 -0500 | [diff] [blame] | 2335 | return ll_obd_statfs(inode, (void __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2336 | |
| 2337 | /* We need to special case any other ioctls we want to handle, |
| 2338 | * to send them to the MDS/OST as appropriate and to properly |
| 2339 | * network encode the arg field. |
| 2340 | case FSFILT_IOC_SETVERSION_OLD: |
| 2341 | case FSFILT_IOC_SETVERSION: |
| 2342 | */ |
| 2343 | case LL_IOC_FLUSHCTX: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2344 | return ll_flush_ctx(inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2345 | case LL_IOC_PATH2FID: { |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2346 | if (copy_to_user((void __user *)arg, ll_inode2fid(inode), |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2347 | sizeof(struct lu_fid))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2348 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2349 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2350 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2351 | } |
| 2352 | case OBD_IOC_FID2PATH: |
Oleg Drokin | 61dad0b | 2016-01-03 12:05:56 -0500 | [diff] [blame] | 2353 | return ll_fid2path(inode, (void __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2354 | case LL_IOC_DATA_VERSION: { |
| 2355 | struct ioc_data_version idv; |
| 2356 | int rc; |
| 2357 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2358 | if (copy_from_user(&idv, (char __user *)arg, sizeof(idv))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2359 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2360 | |
Jinshan Xiong | e179800 | 2016-04-04 21:37:01 -0400 | [diff] [blame] | 2361 | idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH; |
| 2362 | rc = ll_data_version(inode, &idv.idv_version, idv.idv_flags); |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2363 | if (rc == 0 && copy_to_user((char __user *)arg, &idv, |
| 2364 | sizeof(idv))) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2365 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2366 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2367 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | case LL_IOC_GET_MDTIDX: { |
| 2371 | int mdtidx; |
| 2372 | |
| 2373 | mdtidx = ll_get_mdt_idx(inode); |
| 2374 | if (mdtidx < 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2375 | return mdtidx; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2376 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2377 | if (put_user(mdtidx, (int __user *)arg)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2378 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2379 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2380 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2381 | } |
| 2382 | case OBD_IOC_GETDTNAME: |
| 2383 | case OBD_IOC_GETMDNAME: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2384 | return ll_get_obd_name(inode, cmd, arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2385 | case LL_IOC_HSM_STATE_GET: { |
| 2386 | struct md_op_data *op_data; |
| 2387 | struct hsm_user_state *hus; |
| 2388 | int rc; |
| 2389 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 2390 | hus = kzalloc(sizeof(*hus), GFP_NOFS); |
| 2391 | if (!hus) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2392 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2393 | |
| 2394 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0, |
| 2395 | LUSTRE_OPC_ANY, hus); |
John L. Hammond | 79a8726 | 2013-07-23 00:06:29 +0800 | [diff] [blame] | 2396 | if (IS_ERR(op_data)) { |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2397 | kfree(hus); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2398 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data), |
| 2402 | op_data, NULL); |
| 2403 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2404 | if (copy_to_user((void __user *)arg, hus, sizeof(*hus))) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2405 | rc = -EFAULT; |
| 2406 | |
| 2407 | ll_finish_md_op_data(op_data); |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2408 | kfree(hus); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2409 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2410 | } |
| 2411 | case LL_IOC_HSM_STATE_SET: { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2412 | struct hsm_state_set *hss; |
| 2413 | int rc; |
| 2414 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2415 | hss = memdup_user((char __user *)arg, sizeof(*hss)); |
Abdul Hussain | 0c027bc | 2015-06-16 07:04:51 +0000 | [diff] [blame] | 2416 | if (IS_ERR(hss)) |
| 2417 | return PTR_ERR(hss); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2418 | |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2419 | rc = ll_hsm_state_set(inode, hss); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2420 | |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2421 | kfree(hss); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2422 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2423 | } |
| 2424 | case LL_IOC_HSM_ACTION: { |
| 2425 | struct md_op_data *op_data; |
| 2426 | struct hsm_current_action *hca; |
| 2427 | int rc; |
| 2428 | |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 2429 | hca = kzalloc(sizeof(*hca), GFP_NOFS); |
| 2430 | if (!hca) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2431 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2432 | |
| 2433 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0, |
| 2434 | LUSTRE_OPC_ANY, hca); |
John L. Hammond | 79a8726 | 2013-07-23 00:06:29 +0800 | [diff] [blame] | 2435 | if (IS_ERR(op_data)) { |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2436 | kfree(hca); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2437 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data), |
| 2441 | op_data, NULL); |
| 2442 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2443 | if (copy_to_user((char __user *)arg, hca, sizeof(*hca))) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2444 | rc = -EFAULT; |
| 2445 | |
| 2446 | ll_finish_md_op_data(op_data); |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2447 | kfree(hca); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2448 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2449 | } |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2450 | case LL_IOC_SET_LEASE: { |
| 2451 | struct ll_inode_info *lli = ll_i2info(inode); |
| 2452 | struct obd_client_handle *och = NULL; |
| 2453 | bool lease_broken; |
| 2454 | fmode_t mode = 0; |
| 2455 | |
| 2456 | switch (arg) { |
| 2457 | case F_WRLCK: |
| 2458 | if (!(file->f_mode & FMODE_WRITE)) |
| 2459 | return -EPERM; |
| 2460 | mode = FMODE_WRITE; |
| 2461 | break; |
| 2462 | case F_RDLCK: |
| 2463 | if (!(file->f_mode & FMODE_READ)) |
| 2464 | return -EPERM; |
| 2465 | mode = FMODE_READ; |
| 2466 | break; |
| 2467 | case F_UNLCK: |
| 2468 | mutex_lock(&lli->lli_och_mutex); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2469 | if (fd->fd_lease_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2470 | och = fd->fd_lease_och; |
| 2471 | fd->fd_lease_och = NULL; |
| 2472 | } |
| 2473 | mutex_unlock(&lli->lli_och_mutex); |
| 2474 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2475 | if (och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2476 | mode = och->och_flags & |
| 2477 | (FMODE_READ|FMODE_WRITE); |
| 2478 | rc = ll_lease_close(och, inode, &lease_broken); |
| 2479 | if (rc == 0 && lease_broken) |
| 2480 | mode = 0; |
| 2481 | } else { |
| 2482 | rc = -ENOLCK; |
| 2483 | } |
| 2484 | |
| 2485 | /* return the type of lease or error */ |
| 2486 | return rc < 0 ? rc : (int)mode; |
| 2487 | default: |
| 2488 | return -EINVAL; |
| 2489 | } |
| 2490 | |
| 2491 | CDEBUG(D_INODE, "Set lease with mode %d\n", mode); |
| 2492 | |
| 2493 | /* apply for lease */ |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 2494 | och = ll_lease_open(inode, file, mode, 0); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2495 | if (IS_ERR(och)) |
| 2496 | return PTR_ERR(och); |
| 2497 | |
| 2498 | rc = 0; |
| 2499 | mutex_lock(&lli->lli_och_mutex); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2500 | if (!fd->fd_lease_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2501 | fd->fd_lease_och = och; |
| 2502 | och = NULL; |
| 2503 | } |
| 2504 | mutex_unlock(&lli->lli_och_mutex); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2505 | if (och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2506 | /* impossible now that only excl is supported for now */ |
| 2507 | ll_lease_close(och, inode, &lease_broken); |
| 2508 | rc = -EBUSY; |
| 2509 | } |
| 2510 | return rc; |
| 2511 | } |
| 2512 | case LL_IOC_GET_LEASE: { |
| 2513 | struct ll_inode_info *lli = ll_i2info(inode); |
| 2514 | struct ldlm_lock *lock = NULL; |
| 2515 | |
| 2516 | rc = 0; |
| 2517 | mutex_lock(&lli->lli_och_mutex); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2518 | if (fd->fd_lease_och) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2519 | struct obd_client_handle *och = fd->fd_lease_och; |
| 2520 | |
| 2521 | lock = ldlm_handle2lock(&och->och_lease_handle); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 2522 | if (lock) { |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2523 | lock_res_and_lock(lock); |
| 2524 | if (!ldlm_is_cancel(lock)) |
| 2525 | rc = och->och_flags & |
| 2526 | (FMODE_READ | FMODE_WRITE); |
| 2527 | unlock_res_and_lock(lock); |
Jinshan Xiong | ead0280 | 2016-04-04 21:36:51 -0400 | [diff] [blame] | 2528 | LDLM_LOCK_PUT(lock); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2529 | } |
| 2530 | } |
| 2531 | mutex_unlock(&lli->lli_och_mutex); |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2532 | return rc; |
| 2533 | } |
| 2534 | case LL_IOC_HSM_IMPORT: { |
| 2535 | struct hsm_user_import *hui; |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2536 | |
Oleg Drokin | 02f9c12 | 2016-01-03 12:05:55 -0500 | [diff] [blame] | 2537 | hui = memdup_user((void __user *)arg, sizeof(*hui)); |
Abdul Hussain | 0c027bc | 2015-06-16 07:04:51 +0000 | [diff] [blame] | 2538 | if (IS_ERR(hui)) |
| 2539 | return PTR_ERR(hui); |
JC Lafoucriere | a720b79 | 2013-12-09 22:56:55 +0800 | [diff] [blame] | 2540 | |
| 2541 | rc = ll_hsm_import(inode, file, hui); |
| 2542 | |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 2543 | kfree(hui); |
Jinshan Xiong | d3a8a4e | 2013-11-19 21:23:41 +0800 | [diff] [blame] | 2544 | return rc; |
| 2545 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2546 | default: { |
| 2547 | int err; |
| 2548 | |
Julia Lawall | 1f6eaf8 | 2015-08-29 19:30:09 +0200 | [diff] [blame] | 2549 | if (ll_iocontrol_call(inode, file, cmd, arg, &err) == |
| 2550 | LLIOC_STOP) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2551 | return err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2552 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2553 | return obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL, |
Oleg Drokin | e09bee3 | 2016-01-03 12:05:43 -0500 | [diff] [blame] | 2554 | (void __user *)arg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2559 | static loff_t ll_file_seek(struct file *file, loff_t offset, int origin) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2560 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2561 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2562 | loff_t retval, eof = 0; |
| 2563 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2564 | retval = offset + ((origin == SEEK_END) ? i_size_read(inode) : |
| 2565 | (origin == SEEK_CUR) ? file->f_pos : 0); |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2566 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n", |
| 2567 | PFID(ll_inode2fid(inode)), inode, retval, retval, origin); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2568 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1); |
| 2569 | |
| 2570 | if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) { |
| 2571 | retval = ll_glimpse_size(inode); |
| 2572 | if (retval != 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2573 | return retval; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2574 | eof = i_size_read(inode); |
| 2575 | } |
| 2576 | |
Greg Kroah-Hartman | 6f01433 | 2013-08-02 15:56:22 +0800 | [diff] [blame] | 2577 | retval = generic_file_llseek_size(file, offset, origin, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2578 | ll_file_maxbytes(inode), eof); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2579 | return retval; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2580 | } |
| 2581 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2582 | static int ll_flush(struct file *file, fl_owner_t id) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2583 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2584 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2585 | struct ll_inode_info *lli = ll_i2info(inode); |
| 2586 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 2587 | int rc, err; |
| 2588 | |
| 2589 | LASSERT(!S_ISDIR(inode->i_mode)); |
| 2590 | |
| 2591 | /* catch async errors that were recorded back when async writeback |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2592 | * failed for pages in this mapping. |
| 2593 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2594 | rc = lli->lli_async_rc; |
| 2595 | lli->lli_async_rc = 0; |
| 2596 | err = lov_read_and_clear_async_rc(lli->lli_clob); |
| 2597 | if (rc == 0) |
| 2598 | rc = err; |
| 2599 | |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2600 | /* The application has been told about write failure already. |
| 2601 | * Do not report failure again. |
| 2602 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2603 | if (fd->fd_write_failed) |
| 2604 | return 0; |
| 2605 | return rc ? -EIO : 0; |
| 2606 | } |
| 2607 | |
| 2608 | /** |
| 2609 | * Called to make sure a portion of file has been written out. |
Andreas Dilger | 0528992 | 2014-04-27 13:07:09 -0400 | [diff] [blame] | 2610 | * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2611 | * |
| 2612 | * Return how many pages have been written. |
| 2613 | */ |
| 2614 | int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end, |
Niu Yawei | 65fb55d | 2013-06-03 21:40:38 +0800 | [diff] [blame] | 2615 | enum cl_fsync_mode mode, int ignore_layout) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2616 | { |
| 2617 | struct cl_env_nest nest; |
| 2618 | struct lu_env *env; |
| 2619 | struct cl_io *io; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2620 | struct cl_fsync_io *fio; |
| 2621 | int result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2622 | |
| 2623 | if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL && |
| 2624 | mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2625 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2626 | |
| 2627 | env = cl_env_nested_get(&nest); |
| 2628 | if (IS_ERR(env)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2629 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2630 | |
John Hammond | 9acc450 | 2016-03-30 19:48:57 -0400 | [diff] [blame] | 2631 | io = vvp_env_thread_io(env); |
John L. Hammond | 1929c43 | 2016-03-30 19:48:37 -0400 | [diff] [blame] | 2632 | io->ci_obj = ll_i2info(inode)->lli_clob; |
Niu Yawei | 65fb55d | 2013-06-03 21:40:38 +0800 | [diff] [blame] | 2633 | io->ci_ignore_layout = ignore_layout; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2634 | |
| 2635 | /* initialize parameters for sync */ |
| 2636 | fio = &io->u.ci_fsync; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2637 | fio->fi_start = start; |
| 2638 | fio->fi_end = end; |
| 2639 | fio->fi_fid = ll_inode2fid(inode); |
| 2640 | fio->fi_mode = mode; |
| 2641 | fio->fi_nr_written = 0; |
| 2642 | |
| 2643 | if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0) |
| 2644 | result = cl_io_loop(env, io); |
| 2645 | else |
| 2646 | result = io->ci_result; |
| 2647 | if (result == 0) |
| 2648 | result = fio->fi_nr_written; |
| 2649 | cl_io_fini(env, io); |
| 2650 | cl_env_nested_put(&nest, env); |
| 2651 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2652 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2653 | } |
| 2654 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2655 | int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 2656 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2657 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2658 | struct ll_inode_info *lli = ll_i2info(inode); |
| 2659 | struct ptlrpc_request *req; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2660 | int rc, err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2661 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2662 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n", |
| 2663 | PFID(ll_inode2fid(inode)), inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2664 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1); |
| 2665 | |
| 2666 | rc = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2667 | inode_lock(inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2668 | |
| 2669 | /* catch async errors that were recorded back when async writeback |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2670 | * failed for pages in this mapping. |
| 2671 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2672 | if (!S_ISDIR(inode->i_mode)) { |
| 2673 | err = lli->lli_async_rc; |
| 2674 | lli->lli_async_rc = 0; |
| 2675 | if (rc == 0) |
| 2676 | rc = err; |
| 2677 | err = lov_read_and_clear_async_rc(lli->lli_clob); |
| 2678 | if (rc == 0) |
| 2679 | rc = err; |
| 2680 | } |
| 2681 | |
Oleg Drokin | ef2e0f5 | 2015-09-27 16:45:46 -0400 | [diff] [blame] | 2682 | err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2683 | if (!rc) |
| 2684 | rc = err; |
| 2685 | if (!err) |
| 2686 | ptlrpc_req_finished(req); |
| 2687 | |
Bobi Jam | 8d97deb | 2014-04-27 13:06:44 -0400 | [diff] [blame] | 2688 | if (S_ISREG(inode->i_mode)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2689 | struct ll_file_data *fd = LUSTRE_FPRIVATE(file); |
| 2690 | |
Andreas Dilger | 0528992 | 2014-04-27 13:07:09 -0400 | [diff] [blame] | 2691 | err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2692 | if (rc == 0 && err < 0) |
| 2693 | rc = err; |
| 2694 | if (rc < 0) |
| 2695 | fd->fd_write_failed = true; |
| 2696 | else |
| 2697 | fd->fd_write_failed = false; |
| 2698 | } |
| 2699 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2700 | inode_unlock(inode); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2701 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2702 | } |
| 2703 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2704 | static int |
| 2705 | ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2706 | { |
Al Viro | 2a8a359 | 2014-10-20 18:02:33 -0400 | [diff] [blame] | 2707 | struct inode *inode = file_inode(file); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2708 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
Bruce Korb | f2145ea | 2013-07-23 00:06:37 +0800 | [diff] [blame] | 2709 | struct ldlm_enqueue_info einfo = { |
| 2710 | .ei_type = LDLM_FLOCK, |
| 2711 | .ei_cb_cp = ldlm_flock_completion_ast, |
| 2712 | .ei_cbdata = file_lock, |
| 2713 | }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2714 | struct md_op_data *op_data; |
| 2715 | struct lustre_handle lockh = {0}; |
Anjali Menon | 8369cff | 2015-08-18 19:24:24 +0530 | [diff] [blame] | 2716 | ldlm_policy_data_t flock = { {0} }; |
Dmitry Eremin | 875332d | 2014-06-22 21:32:08 -0400 | [diff] [blame] | 2717 | __u64 flags = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2718 | int rc; |
| 2719 | int rc2 = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2720 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2721 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n", |
| 2722 | PFID(ll_inode2fid(inode)), file_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2723 | |
| 2724 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1); |
| 2725 | |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 2726 | if (file_lock->fl_flags & FL_FLOCK) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2727 | LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK)); |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 2728 | else if (!(file_lock->fl_flags & FL_POSIX)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2729 | return -EINVAL; |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 2730 | |
| 2731 | flock.l_flock.owner = (unsigned long)file_lock->fl_owner; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2732 | flock.l_flock.pid = file_lock->fl_pid; |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 2733 | flock.l_flock.start = file_lock->fl_start; |
| 2734 | flock.l_flock.end = file_lock->fl_end; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2735 | |
| 2736 | /* Somewhat ugly workaround for svc lockd. |
| 2737 | * lockd installs custom fl_lmops->lm_compare_owner that checks |
| 2738 | * for the fl_owner to be the same (which it always is on local node |
| 2739 | * I guess between lockd processes) and then compares pid. |
| 2740 | * As such we assign pid to the owner field to make it all work, |
| 2741 | * conflict with normal locks is unlikely since pid space and |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2742 | * pointer space for current->files are not intersecting |
| 2743 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2744 | if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner) |
| 2745 | flock.l_flock.owner = (unsigned long)file_lock->fl_pid; |
| 2746 | |
| 2747 | switch (file_lock->fl_type) { |
| 2748 | case F_RDLCK: |
| 2749 | einfo.ei_mode = LCK_PR; |
| 2750 | break; |
| 2751 | case F_UNLCK: |
| 2752 | /* An unlock request may or may not have any relation to |
| 2753 | * existing locks so we may not be able to pass a lock handle |
| 2754 | * via a normal ldlm_lock_cancel() request. The request may even |
| 2755 | * unlock a byte range in the middle of an existing lock. In |
| 2756 | * order to process an unlock request we need all of the same |
| 2757 | * information that is given with a normal read or write record |
| 2758 | * lock request. To avoid creating another ldlm unlock (cancel) |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2759 | * message we'll treat a LCK_NL flock request as an unlock. |
| 2760 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2761 | einfo.ei_mode = LCK_NL; |
| 2762 | break; |
| 2763 | case F_WRLCK: |
| 2764 | einfo.ei_mode = LCK_PW; |
| 2765 | break; |
| 2766 | default: |
| 2767 | CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 2768 | file_lock->fl_type); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2769 | return -ENOTSUPP; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | switch (cmd) { |
| 2773 | case F_SETLKW: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2774 | #ifdef F_SETLKW64 |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2775 | case F_SETLKW64: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2776 | #endif |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2777 | flags = 0; |
| 2778 | break; |
| 2779 | case F_SETLK: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2780 | #ifdef F_SETLK64 |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2781 | case F_SETLK64: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2782 | #endif |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2783 | flags = LDLM_FL_BLOCK_NOWAIT; |
| 2784 | break; |
| 2785 | case F_GETLK: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2786 | #ifdef F_GETLK64 |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2787 | case F_GETLK64: |
Greg Kroah-Hartman | 2faedcd | 2016-03-02 12:06:42 -0800 | [diff] [blame] | 2788 | #endif |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2789 | flags = LDLM_FL_TEST_LOCK; |
| 2790 | /* Save the old mode so that if the mode in the lock changes we |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2791 | * can decrement the appropriate reader or writer refcount. |
| 2792 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2793 | file_lock->fl_type = einfo.ei_mode; |
| 2794 | break; |
| 2795 | default: |
| 2796 | CERROR("unknown fcntl lock command: %d\n", cmd); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2797 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0, |
| 2801 | LUSTRE_OPC_ANY, NULL); |
| 2802 | if (IS_ERR(op_data)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2803 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2804 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2805 | CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, start=%llu, end=%llu\n", |
| 2806 | PFID(ll_inode2fid(inode)), flock.l_flock.pid, flags, |
| 2807 | einfo.ei_mode, flock.l_flock.start, flock.l_flock.end); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2808 | |
| 2809 | rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL, |
| 2810 | op_data, &lockh, &flock, 0, NULL /* req */, flags); |
| 2811 | |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 2812 | if ((rc == 0 || file_lock->fl_type == F_UNLCK) && |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2813 | !(flags & LDLM_FL_TEST_LOCK)) |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 2814 | rc2 = locks_lock_file_wait(file, file_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2815 | |
| 2816 | if (rc2 && file_lock->fl_type != F_UNLCK) { |
| 2817 | einfo.ei_mode = LCK_NL; |
| 2818 | md_enqueue(sbi->ll_md_exp, &einfo, NULL, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 2819 | op_data, &lockh, &flock, 0, NULL /* req */, flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2820 | rc = rc2; |
| 2821 | } |
| 2822 | |
| 2823 | ll_finish_md_op_data(op_data); |
| 2824 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2825 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2826 | } |
| 2827 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2828 | static int |
| 2829 | ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2830 | { |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2831 | return -ENOSYS; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | /** |
| 2835 | * test if some locks matching bits and l_req_mode are acquired |
| 2836 | * - bits can be in different locks |
| 2837 | * - if found clear the common lock bits in *bits |
| 2838 | * - the bits not found, are kept in *bits |
| 2839 | * \param inode [IN] |
| 2840 | * \param bits [IN] searched lock bits [IN] |
| 2841 | * \param l_req_mode [IN] searched lock mode |
| 2842 | * \retval boolean, true iff all bits are found |
| 2843 | */ |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 2844 | int ll_have_md_lock(struct inode *inode, __u64 *bits, |
| 2845 | enum ldlm_mode l_req_mode) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2846 | { |
| 2847 | struct lustre_handle lockh; |
| 2848 | ldlm_policy_data_t policy; |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 2849 | enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ? |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2850 | (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode; |
| 2851 | struct lu_fid *fid; |
| 2852 | __u64 flags; |
| 2853 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2854 | |
| 2855 | if (!inode) |
aybuke ozdemir | ef075ed | 2015-02-26 23:45:46 +0200 | [diff] [blame] | 2856 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2857 | |
| 2858 | fid = &ll_i2info(inode)->lli_fid; |
| 2859 | CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid), |
| 2860 | ldlm_lockname[mode]); |
| 2861 | |
| 2862 | flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK; |
wang di | 1253b2e | 2013-07-23 00:06:25 +0800 | [diff] [blame] | 2863 | for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2864 | policy.l_inodebits.bits = *bits & (1 << i); |
| 2865 | if (policy.l_inodebits.bits == 0) |
| 2866 | continue; |
| 2867 | |
| 2868 | if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, |
| 2869 | &policy, mode, &lockh)) { |
| 2870 | struct ldlm_lock *lock; |
| 2871 | |
| 2872 | lock = ldlm_handle2lock(&lockh); |
| 2873 | if (lock) { |
| 2874 | *bits &= |
| 2875 | ~(lock->l_policy_data.l_inodebits.bits); |
| 2876 | LDLM_LOCK_PUT(lock); |
| 2877 | } else { |
| 2878 | *bits &= ~policy.l_inodebits.bits; |
| 2879 | } |
| 2880 | } |
| 2881 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2882 | return *bits == 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2883 | } |
| 2884 | |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 2885 | enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits, |
| 2886 | struct lustre_handle *lockh, __u64 flags, |
| 2887 | enum ldlm_mode mode) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2888 | { |
Tina Ruchandani | 57303e7 | 2014-10-21 22:13:21 -0700 | [diff] [blame] | 2889 | ldlm_policy_data_t policy = { .l_inodebits = {bits} }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2890 | struct lu_fid *fid; |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 2891 | enum ldlm_mode rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2892 | |
| 2893 | fid = &ll_i2info(inode)->lli_fid; |
| 2894 | CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid)); |
| 2895 | |
Julia Lawall | 1f6eaf8 | 2015-08-29 19:30:09 +0200 | [diff] [blame] | 2896 | rc = md_lock_match(ll_i2mdexp(inode), flags | LDLM_FL_BLOCK_GRANTED, |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 2897 | fid, LDLM_IBITS, &policy, mode, lockh); |
| 2898 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2899 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | static int ll_inode_revalidate_fini(struct inode *inode, int rc) |
| 2903 | { |
| 2904 | /* Already unlinked. Just update nlink and return success */ |
| 2905 | if (rc == -ENOENT) { |
| 2906 | clear_nlink(inode); |
| 2907 | /* This path cannot be hit for regular files unless in |
Masanari Iida | bef31c7 | 2013-09-15 14:38:18 +0900 | [diff] [blame] | 2908 | * case of obscure races, so no need to validate size. |
| 2909 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2910 | if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) |
| 2911 | return 0; |
| 2912 | } else if (rc != 0) { |
Andreas Dilger | e49634b | 2014-04-27 13:06:38 -0400 | [diff] [blame] | 2913 | CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR, |
| 2914 | "%s: revalidate FID "DFID" error: rc = %d\n", |
| 2915 | ll_get_fsname(inode->i_sb, NULL, 0), |
| 2916 | PFID(ll_inode2fid(inode)), rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | return rc; |
| 2920 | } |
| 2921 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 2922 | static int __ll_inode_revalidate(struct dentry *dentry, __u64 ibits) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2923 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2924 | struct inode *inode = d_inode(dentry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2925 | struct ptlrpc_request *req = NULL; |
| 2926 | struct obd_export *exp; |
| 2927 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2928 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 2929 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%pd\n", |
| 2930 | PFID(ll_inode2fid(inode)), inode, dentry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2931 | |
| 2932 | exp = ll_i2mdexp(inode); |
| 2933 | |
| 2934 | /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC. |
| 2935 | * But under CMD case, it caused some lock issues, should be fixed |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2936 | * with new CMD ibits lock. See bug 12718 |
| 2937 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2938 | if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) { |
| 2939 | struct lookup_intent oit = { .it_op = IT_GETATTR }; |
| 2940 | struct md_op_data *op_data; |
| 2941 | |
| 2942 | if (ibits == MDS_INODELOCK_LOOKUP) |
| 2943 | oit.it_op = IT_LOOKUP; |
| 2944 | |
| 2945 | /* Call getattr by fid, so do not provide name at all. */ |
Al Viro | dbca51d | 2015-01-18 23:31:19 -0500 | [diff] [blame] | 2946 | op_data = ll_prep_md_op_data(NULL, inode, |
| 2947 | inode, NULL, 0, 0, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2948 | LUSTRE_OPC_ANY, NULL); |
| 2949 | if (IS_ERR(op_data)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2950 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2951 | |
| 2952 | oit.it_create_mode |= M_CHECK_STALE; |
| 2953 | rc = md_intent_lock(exp, op_data, NULL, 0, |
| 2954 | /* we are not interested in name |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2955 | * based lookup |
| 2956 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2957 | &oit, 0, &req, |
| 2958 | ll_md_blocking_ast, 0); |
| 2959 | ll_finish_md_op_data(op_data); |
| 2960 | oit.it_create_mode &= ~M_CHECK_STALE; |
| 2961 | if (rc < 0) { |
| 2962 | rc = ll_inode_revalidate_fini(inode, rc); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2963 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2964 | } |
| 2965 | |
Al Viro | dbca51d | 2015-01-18 23:31:19 -0500 | [diff] [blame] | 2966 | rc = ll_revalidate_it_finish(req, &oit, inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2967 | if (rc != 0) { |
| 2968 | ll_intent_release(&oit); |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 2969 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | /* Unlinked? Unhash dentry, so it is not picked up later by |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 2973 | * do_lookup() -> ll_revalidate_it(). We cannot use d_drop |
| 2974 | * here to preserve get_cwd functionality on 2.6. |
| 2975 | * Bug 10503 |
| 2976 | */ |
Bruno Faccini | c9cc8d0 | 2016-06-20 16:55:27 -0400 | [diff] [blame] | 2977 | if (!d_inode(dentry)->i_nlink) { |
| 2978 | spin_lock(&inode->i_lock); |
Peng Tao | b1d2a12 | 2013-06-03 21:58:15 +0800 | [diff] [blame] | 2979 | d_lustre_invalidate(dentry, 0); |
Bruno Faccini | c9cc8d0 | 2016-06-20 16:55:27 -0400 | [diff] [blame] | 2980 | spin_unlock(&inode->i_lock); |
| 2981 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2982 | |
Al Viro | dbca51d | 2015-01-18 23:31:19 -0500 | [diff] [blame] | 2983 | ll_lookup_finish_locks(&oit, inode); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2984 | } else if (!ll_have_md_lock(d_inode(dentry), &ibits, LCK_MINMODE)) { |
| 2985 | struct ll_sb_info *sbi = ll_i2sbi(d_inode(dentry)); |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 2986 | u64 valid = OBD_MD_FLGETATTR; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2987 | struct md_op_data *op_data; |
| 2988 | int ealen = 0; |
| 2989 | |
| 2990 | if (S_ISREG(inode->i_mode)) { |
Brian Behlendorf | 4477934 | 2014-04-27 13:06:47 -0400 | [diff] [blame] | 2991 | rc = ll_get_default_mdsize(sbi, &ealen); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2992 | if (rc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2993 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2994 | valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE; |
| 2995 | } |
| 2996 | |
| 2997 | op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, |
| 2998 | 0, ealen, LUSTRE_OPC_ANY, |
| 2999 | NULL); |
| 3000 | if (IS_ERR(op_data)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3001 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3002 | |
| 3003 | op_data->op_valid = valid; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3004 | rc = md_getattr(sbi->ll_md_exp, op_data, &req); |
| 3005 | ll_finish_md_op_data(op_data); |
| 3006 | if (rc) { |
| 3007 | rc = ll_inode_revalidate_fini(inode, rc); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3008 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | rc = ll_prep_inode(&inode, req, NULL, NULL); |
| 3012 | } |
| 3013 | out: |
| 3014 | ptlrpc_req_finished(req); |
| 3015 | return rc; |
| 3016 | } |
| 3017 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3018 | static int ll_inode_revalidate(struct dentry *dentry, __u64 ibits) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3019 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3020 | struct inode *inode = d_inode(dentry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3021 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3022 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3023 | rc = __ll_inode_revalidate(dentry, ibits); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3024 | if (rc != 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3025 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3026 | |
| 3027 | /* if object isn't regular file, don't validate size */ |
| 3028 | if (!S_ISREG(inode->i_mode)) { |
John L. Hammond | d299573 | 2016-03-30 19:48:34 -0400 | [diff] [blame] | 3029 | LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_atime; |
| 3030 | LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_mtime; |
| 3031 | LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_ctime; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3032 | } else { |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3033 | /* In case of restore, the MDT has the right size and has |
| 3034 | * already send it back without granting the layout lock, |
| 3035 | * inode is up-to-date so glimpse is useless. |
| 3036 | * Also to glimpse we need the layout, in case of a running |
| 3037 | * restore the MDT holds the layout lock so the glimpse will |
| 3038 | * block up to the end of restore (getattr will block) |
| 3039 | */ |
| 3040 | if (!(ll_i2info(inode)->lli_flags & LLIF_FILE_RESTORING)) |
| 3041 | rc = ll_glimpse_size(inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3042 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3043 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3044 | } |
| 3045 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3046 | int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3047 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3048 | struct inode *inode = d_inode(de); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3049 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 3050 | struct ll_inode_info *lli = ll_i2info(inode); |
Julia Lawall | f82ced5 | 2015-06-20 21:07:50 +0200 | [diff] [blame] | 3051 | int res; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3052 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3053 | res = ll_inode_revalidate(de, MDS_INODELOCK_UPDATE | |
| 3054 | MDS_INODELOCK_LOOKUP); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3055 | ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1); |
| 3056 | |
| 3057 | if (res) |
| 3058 | return res; |
| 3059 | |
| 3060 | stat->dev = inode->i_sb->s_dev; |
| 3061 | if (ll_need_32bit_api(sbi)) |
| 3062 | stat->ino = cl_fid_build_ino(&lli->lli_fid, 1); |
| 3063 | else |
| 3064 | stat->ino = inode->i_ino; |
| 3065 | stat->mode = inode->i_mode; |
| 3066 | stat->nlink = inode->i_nlink; |
| 3067 | stat->uid = inode->i_uid; |
| 3068 | stat->gid = inode->i_gid; |
| 3069 | stat->rdev = inode->i_rdev; |
| 3070 | stat->atime = inode->i_atime; |
| 3071 | stat->mtime = inode->i_mtime; |
| 3072 | stat->ctime = inode->i_ctime; |
| 3073 | stat->blksize = 1 << inode->i_blkbits; |
| 3074 | |
| 3075 | stat->size = i_size_read(inode); |
| 3076 | stat->blocks = inode->i_blocks; |
| 3077 | |
| 3078 | return 0; |
| 3079 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3080 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3081 | static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 3082 | __u64 start, __u64 len) |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3083 | { |
| 3084 | int rc; |
| 3085 | size_t num_bytes; |
| 3086 | struct ll_user_fiemap *fiemap; |
| 3087 | unsigned int extent_count = fieinfo->fi_extents_max; |
| 3088 | |
| 3089 | num_bytes = sizeof(*fiemap) + (extent_count * |
| 3090 | sizeof(struct ll_fiemap_extent)); |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 3091 | fiemap = libcfs_kvzalloc(num_bytes, GFP_NOFS); |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3092 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3093 | if (!fiemap) |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3094 | return -ENOMEM; |
| 3095 | |
| 3096 | fiemap->fm_flags = fieinfo->fi_flags; |
| 3097 | fiemap->fm_extent_count = fieinfo->fi_extents_max; |
| 3098 | fiemap->fm_start = start; |
| 3099 | fiemap->fm_length = len; |
Oleg Drokin | 9751424 | 2016-01-03 12:05:48 -0500 | [diff] [blame] | 3100 | if (extent_count > 0 && |
| 3101 | copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start, |
| 3102 | sizeof(struct ll_fiemap_extent)) != 0) { |
| 3103 | rc = -EFAULT; |
| 3104 | goto out; |
| 3105 | } |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3106 | |
| 3107 | rc = ll_do_fiemap(inode, fiemap, num_bytes); |
| 3108 | |
| 3109 | fieinfo->fi_flags = fiemap->fm_flags; |
| 3110 | fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents; |
Oleg Drokin | 9751424 | 2016-01-03 12:05:48 -0500 | [diff] [blame] | 3111 | if (extent_count > 0 && |
| 3112 | copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0], |
| 3113 | fiemap->fm_mapped_extents * |
| 3114 | sizeof(struct ll_fiemap_extent)) != 0) { |
| 3115 | rc = -EFAULT; |
| 3116 | goto out; |
| 3117 | } |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3118 | |
Oleg Drokin | 9751424 | 2016-01-03 12:05:48 -0500 | [diff] [blame] | 3119 | out: |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 3120 | kvfree(fiemap); |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3121 | return rc; |
| 3122 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3123 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3124 | struct posix_acl *ll_get_acl(struct inode *inode, int type) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3125 | { |
| 3126 | struct ll_inode_info *lli = ll_i2info(inode); |
| 3127 | struct posix_acl *acl = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3128 | |
| 3129 | spin_lock(&lli->lli_lock); |
| 3130 | /* VFS' acl_permission_check->check_acl will release the refcount */ |
| 3131 | acl = posix_acl_dup(lli->lli_posix_acl); |
Arnd Bergmann | ed7bdf5 | 2016-06-13 22:44:57 +0200 | [diff] [blame] | 3132 | #ifdef CONFIG_FS_POSIX_ACL |
James Simmons | b788dc5 | 2016-05-23 20:35:08 -0400 | [diff] [blame] | 3133 | forget_cached_acl(inode, type); |
Arnd Bergmann | ed7bdf5 | 2016-06-13 22:44:57 +0200 | [diff] [blame] | 3134 | #endif |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3135 | spin_unlock(&lli->lli_lock); |
| 3136 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3137 | return acl; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3138 | } |
| 3139 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3140 | int ll_inode_permission(struct inode *inode, int mask) |
| 3141 | { |
| 3142 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3143 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3144 | if (mask & MAY_NOT_BLOCK) |
| 3145 | return -ECHILD; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3146 | |
| 3147 | /* as root inode are NOT getting validated in lookup operation, |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3148 | * need to do it before permission check. |
| 3149 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3150 | |
Al Viro | f76c23d | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 3151 | if (is_root_inode(inode)) { |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3152 | rc = __ll_inode_revalidate(inode->i_sb->s_root, |
| 3153 | MDS_INODELOCK_LOOKUP); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3154 | if (rc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3155 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3156 | } |
| 3157 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3158 | CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n", |
| 3159 | PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3160 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3161 | ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1); |
Greg Kroah-Hartman | 8707c96 | 2013-08-02 16:08:01 +0800 | [diff] [blame] | 3162 | rc = generic_permission(inode, mask); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3163 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3164 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3165 | } |
| 3166 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3167 | /* -o localflock - only provides locally consistent flock locks */ |
| 3168 | struct file_operations ll_file_operations = { |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3169 | .read_iter = ll_file_read_iter, |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3170 | .write_iter = ll_file_write_iter, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3171 | .unlocked_ioctl = ll_file_ioctl, |
| 3172 | .open = ll_file_open, |
| 3173 | .release = ll_file_release, |
| 3174 | .mmap = ll_file_mmap, |
| 3175 | .llseek = ll_file_seek, |
| 3176 | .splice_read = ll_file_splice_read, |
| 3177 | .fsync = ll_fsync, |
| 3178 | .flush = ll_flush |
| 3179 | }; |
| 3180 | |
| 3181 | struct file_operations ll_file_operations_flock = { |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3182 | .read_iter = ll_file_read_iter, |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3183 | .write_iter = ll_file_write_iter, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3184 | .unlocked_ioctl = ll_file_ioctl, |
| 3185 | .open = ll_file_open, |
| 3186 | .release = ll_file_release, |
| 3187 | .mmap = ll_file_mmap, |
| 3188 | .llseek = ll_file_seek, |
| 3189 | .splice_read = ll_file_splice_read, |
| 3190 | .fsync = ll_fsync, |
| 3191 | .flush = ll_flush, |
| 3192 | .flock = ll_file_flock, |
| 3193 | .lock = ll_file_flock |
| 3194 | }; |
| 3195 | |
| 3196 | /* These are for -o noflock - to return ENOSYS on flock calls */ |
| 3197 | struct file_operations ll_file_operations_noflock = { |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3198 | .read_iter = ll_file_read_iter, |
Al Viro | b42b15f | 2014-04-04 12:15:19 -0400 | [diff] [blame] | 3199 | .write_iter = ll_file_write_iter, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3200 | .unlocked_ioctl = ll_file_ioctl, |
| 3201 | .open = ll_file_open, |
| 3202 | .release = ll_file_release, |
| 3203 | .mmap = ll_file_mmap, |
| 3204 | .llseek = ll_file_seek, |
| 3205 | .splice_read = ll_file_splice_read, |
| 3206 | .fsync = ll_fsync, |
| 3207 | .flush = ll_flush, |
| 3208 | .flock = ll_file_noflock, |
| 3209 | .lock = ll_file_noflock |
| 3210 | }; |
| 3211 | |
Julia Lawall | d2d3273 | 2015-11-14 13:30:34 +0100 | [diff] [blame] | 3212 | const struct inode_operations ll_file_inode_operations = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3213 | .setattr = ll_setattr, |
| 3214 | .getattr = ll_getattr, |
| 3215 | .permission = ll_inode_permission, |
| 3216 | .setxattr = ll_setxattr, |
| 3217 | .getxattr = ll_getxattr, |
| 3218 | .listxattr = ll_listxattr, |
| 3219 | .removexattr = ll_removexattr, |
Peng Tao | 89580e3 | 2013-11-15 00:13:03 +0800 | [diff] [blame] | 3220 | .fiemap = ll_fiemap, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3221 | .get_acl = ll_get_acl, |
| 3222 | }; |
| 3223 | |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 3224 | /* dynamic ioctl number support routines */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3225 | static struct llioc_ctl_data { |
| 3226 | struct rw_semaphore ioc_sem; |
| 3227 | struct list_head ioc_head; |
| 3228 | } llioc = { |
| 3229 | __RWSEM_INITIALIZER(llioc.ioc_sem), |
| 3230 | LIST_HEAD_INIT(llioc.ioc_head) |
| 3231 | }; |
| 3232 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3233 | struct llioc_data { |
| 3234 | struct list_head iocd_list; |
| 3235 | unsigned int iocd_size; |
| 3236 | llioc_callback_t iocd_cb; |
| 3237 | unsigned int iocd_count; |
| 3238 | unsigned int iocd_cmd[0]; |
| 3239 | }; |
| 3240 | |
| 3241 | void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd) |
| 3242 | { |
| 3243 | unsigned int size; |
| 3244 | struct llioc_data *in_data = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3245 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3246 | if (!cb || !cmd || count > LLIOC_MAX_CMD || count < 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3247 | return NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3248 | |
| 3249 | size = sizeof(*in_data) + count * sizeof(unsigned int); |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 3250 | in_data = kzalloc(size, GFP_NOFS); |
| 3251 | if (!in_data) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3252 | return NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3253 | |
| 3254 | memset(in_data, 0, sizeof(*in_data)); |
| 3255 | in_data->iocd_size = size; |
| 3256 | in_data->iocd_cb = cb; |
| 3257 | in_data->iocd_count = count; |
| 3258 | memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count); |
| 3259 | |
| 3260 | down_write(&llioc.ioc_sem); |
| 3261 | list_add_tail(&in_data->iocd_list, &llioc.ioc_head); |
| 3262 | up_write(&llioc.ioc_sem); |
| 3263 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3264 | return in_data; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3265 | } |
aybuke ozdemir | 93133eb | 2015-02-26 23:45:44 +0200 | [diff] [blame] | 3266 | EXPORT_SYMBOL(ll_iocontrol_register); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3267 | |
| 3268 | void ll_iocontrol_unregister(void *magic) |
| 3269 | { |
| 3270 | struct llioc_data *tmp; |
| 3271 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3272 | if (!magic) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3273 | return; |
| 3274 | |
| 3275 | down_write(&llioc.ioc_sem); |
| 3276 | list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) { |
| 3277 | if (tmp == magic) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3278 | list_del(&tmp->iocd_list); |
| 3279 | up_write(&llioc.ioc_sem); |
| 3280 | |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 3281 | kfree(tmp); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3282 | return; |
| 3283 | } |
| 3284 | } |
| 3285 | up_write(&llioc.ioc_sem); |
| 3286 | |
| 3287 | CWARN("didn't find iocontrol register block with magic: %p\n", magic); |
| 3288 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3289 | EXPORT_SYMBOL(ll_iocontrol_unregister); |
| 3290 | |
John L. Hammond | 2d95f10 | 2014-04-27 13:07:05 -0400 | [diff] [blame] | 3291 | static enum llioc_iter |
| 3292 | ll_iocontrol_call(struct inode *inode, struct file *file, |
| 3293 | unsigned int cmd, unsigned long arg, int *rcp) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3294 | { |
| 3295 | enum llioc_iter ret = LLIOC_CONT; |
| 3296 | struct llioc_data *data; |
| 3297 | int rc = -EINVAL, i; |
| 3298 | |
| 3299 | down_read(&llioc.ioc_sem); |
| 3300 | list_for_each_entry(data, &llioc.ioc_head, iocd_list) { |
| 3301 | for (i = 0; i < data->iocd_count; i++) { |
| 3302 | if (cmd != data->iocd_cmd[i]) |
| 3303 | continue; |
| 3304 | |
| 3305 | ret = data->iocd_cb(inode, file, cmd, arg, data, &rc); |
| 3306 | break; |
| 3307 | } |
| 3308 | |
| 3309 | if (ret == LLIOC_STOP) |
| 3310 | break; |
| 3311 | } |
| 3312 | up_read(&llioc.ioc_sem); |
| 3313 | |
| 3314 | if (rcp) |
| 3315 | *rcp = rc; |
| 3316 | return ret; |
| 3317 | } |
| 3318 | |
| 3319 | int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf) |
| 3320 | { |
| 3321 | struct ll_inode_info *lli = ll_i2info(inode); |
| 3322 | struct cl_env_nest nest; |
| 3323 | struct lu_env *env; |
| 3324 | int result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3325 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3326 | if (!lli->lli_clob) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3327 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3328 | |
| 3329 | env = cl_env_nested_get(&nest); |
| 3330 | if (IS_ERR(env)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3331 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3332 | |
| 3333 | result = cl_conf_set(env, lli->lli_clob, conf); |
| 3334 | cl_env_nested_put(&nest, env); |
| 3335 | |
| 3336 | if (conf->coc_opc == OBJECT_CONF_SET) { |
| 3337 | struct ldlm_lock *lock = conf->coc_lock; |
| 3338 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3339 | LASSERT(lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3340 | LASSERT(ldlm_has_layout(lock)); |
| 3341 | if (result == 0) { |
| 3342 | /* it can only be allowed to match after layout is |
| 3343 | * applied to inode otherwise false layout would be |
Masanari Iida | d0a0acc | 2014-03-08 22:58:32 +0900 | [diff] [blame] | 3344 | * seen. Applying layout should happen before dropping |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3345 | * the intent lock. |
| 3346 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3347 | ldlm_lock_allow_match(lock); |
| 3348 | } |
| 3349 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3350 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | /* Fetch layout from MDT with getxattr request, if it's not ready yet */ |
| 3354 | static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock) |
| 3355 | |
| 3356 | { |
| 3357 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3358 | struct ptlrpc_request *req; |
| 3359 | struct mdt_body *body; |
| 3360 | void *lvbdata; |
| 3361 | void *lmm; |
| 3362 | int lmmsize; |
| 3363 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3364 | |
jcl | e2335e5 | 2013-07-23 00:06:45 +0800 | [diff] [blame] | 3365 | CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n", |
Bruce Korb | 5a9a80b | 2016-04-27 18:20:57 -0400 | [diff] [blame] | 3366 | PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock), |
jcl | e2335e5 | 2013-07-23 00:06:45 +0800 | [diff] [blame] | 3367 | lock->l_lvb_data, lock->l_lvb_len); |
| 3368 | |
Bruce Korb | 5a9a80b | 2016-04-27 18:20:57 -0400 | [diff] [blame] | 3369 | if (lock->l_lvb_data && ldlm_is_lvb_ready(lock)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3370 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3371 | |
| 3372 | /* if layout lock was granted right away, the layout is returned |
| 3373 | * within DLM_LVB of dlm reply; otherwise if the lock was ever |
| 3374 | * blocked and then granted via completion ast, we have to fetch |
| 3375 | * layout here. Please note that we can't use the LVB buffer in |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3376 | * completion AST because it doesn't have a large enough buffer |
| 3377 | */ |
Brian Behlendorf | 4477934 | 2014-04-27 13:06:47 -0400 | [diff] [blame] | 3378 | rc = ll_get_default_mdsize(sbi, &lmmsize); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3379 | if (rc == 0) |
Oleg Drokin | ef2e0f5 | 2015-09-27 16:45:46 -0400 | [diff] [blame] | 3380 | rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), |
| 3381 | OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0, |
| 3382 | lmmsize, 0, &req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3383 | if (rc < 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3384 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3385 | |
| 3386 | body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3387 | if (!body) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3388 | rc = -EPROTO; |
| 3389 | goto out; |
| 3390 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3391 | |
| 3392 | lmmsize = body->eadatasize; |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3393 | if (lmmsize == 0) /* empty layout */ { |
| 3394 | rc = 0; |
| 3395 | goto out; |
| 3396 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3397 | |
| 3398 | lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3399 | if (!lmm) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3400 | rc = -EFAULT; |
| 3401 | goto out; |
| 3402 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3403 | |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 3404 | lvbdata = libcfs_kvzalloc(lmmsize, GFP_NOFS); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3405 | if (!lvbdata) { |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3406 | rc = -ENOMEM; |
| 3407 | goto out; |
| 3408 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3409 | |
| 3410 | memcpy(lvbdata, lmm, lmmsize); |
| 3411 | lock_res_and_lock(lock); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3412 | if (lock->l_lvb_data) |
Julia Lawall | e958f49 | 2015-06-11 14:02:52 +0200 | [diff] [blame] | 3413 | kvfree(lock->l_lvb_data); |
jcl | e2335e5 | 2013-07-23 00:06:45 +0800 | [diff] [blame] | 3414 | |
| 3415 | lock->l_lvb_data = lvbdata; |
| 3416 | lock->l_lvb_len = lmmsize; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3417 | unlock_res_and_lock(lock); |
| 3418 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3419 | out: |
| 3420 | ptlrpc_req_finished(req); |
| 3421 | return rc; |
| 3422 | } |
| 3423 | |
| 3424 | /** |
| 3425 | * Apply the layout to the inode. Layout lock is held and will be released |
| 3426 | * in this function. |
| 3427 | */ |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 3428 | static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode, |
| 3429 | struct inode *inode, __u32 *gen, bool reconf) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3430 | { |
| 3431 | struct ll_inode_info *lli = ll_i2info(inode); |
| 3432 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 3433 | struct ldlm_lock *lock; |
| 3434 | struct lustre_md md = { NULL }; |
| 3435 | struct cl_object_conf conf; |
| 3436 | int rc = 0; |
| 3437 | bool lvb_ready; |
| 3438 | bool wait_layout = false; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3439 | |
| 3440 | LASSERT(lustre_handle_is_used(lockh)); |
| 3441 | |
| 3442 | lock = ldlm_handle2lock(lockh); |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3443 | LASSERT(lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3444 | LASSERT(ldlm_has_layout(lock)); |
| 3445 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3446 | LDLM_DEBUG(lock, "File "DFID"(%p) being reconfigured: %d", |
| 3447 | PFID(&lli->lli_fid), inode, reconf); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3448 | |
JC Lafoucriere | bc96917 | 2013-06-03 21:40:44 +0800 | [diff] [blame] | 3449 | /* in case this is a caching lock and reinstate with new inode */ |
| 3450 | md_set_lock_data(sbi->ll_md_exp, &lockh->cookie, inode, NULL); |
| 3451 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3452 | lock_res_and_lock(lock); |
Bruce Korb | 5a9a80b | 2016-04-27 18:20:57 -0400 | [diff] [blame] | 3453 | lvb_ready = ldlm_is_lvb_ready(lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3454 | unlock_res_and_lock(lock); |
| 3455 | /* checking lvb_ready is racy but this is okay. The worst case is |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3456 | * that multi processes may configure the file on the same time. |
| 3457 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3458 | if (lvb_ready || !reconf) { |
| 3459 | rc = -ENODATA; |
| 3460 | if (lvb_ready) { |
| 3461 | /* layout_gen must be valid if layout lock is not |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3462 | * cancelled and stripe has already set |
| 3463 | */ |
Jinshan Xiong | 09aed8a | 2014-04-27 13:06:50 -0400 | [diff] [blame] | 3464 | *gen = ll_layout_version_get(lli); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3465 | rc = 0; |
| 3466 | } |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3467 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | rc = ll_layout_fetch(inode, lock); |
| 3471 | if (rc < 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3472 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3473 | |
| 3474 | /* for layout lock, lmm is returned in lock's lvb. |
| 3475 | * lvb_data is immutable if the lock is held so it's safe to access it |
| 3476 | * without res lock. See the description in ldlm_lock_decref_internal() |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3477 | * for the condition to free lvb_data of layout lock |
| 3478 | */ |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3479 | if (lock->l_lvb_data) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3480 | rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm, |
| 3481 | lock->l_lvb_data, lock->l_lvb_len); |
| 3482 | if (rc >= 0) { |
| 3483 | *gen = LL_LAYOUT_GEN_EMPTY; |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3484 | if (md.lsm) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3485 | *gen = md.lsm->lsm_layout_gen; |
| 3486 | rc = 0; |
| 3487 | } else { |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 3488 | CERROR("%s: file " DFID " unpackmd error: %d\n", |
| 3489 | ll_get_fsname(inode->i_sb, NULL, 0), |
| 3490 | PFID(&lli->lli_fid), rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3491 | } |
| 3492 | } |
| 3493 | if (rc < 0) |
Julia Lawall | 34e1f2b | 2014-08-30 16:24:55 +0200 | [diff] [blame] | 3494 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3495 | |
| 3496 | /* set layout to file. Unlikely this will fail as old layout was |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3497 | * surely eliminated |
| 3498 | */ |
Joe Perches | ec83e61 | 2013-10-13 20:22:03 -0700 | [diff] [blame] | 3499 | memset(&conf, 0, sizeof(conf)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3500 | conf.coc_opc = OBJECT_CONF_SET; |
| 3501 | conf.coc_inode = inode; |
| 3502 | conf.coc_lock = lock; |
| 3503 | conf.u.coc_md = &md; |
| 3504 | rc = ll_layout_conf(inode, &conf); |
| 3505 | |
Oleg Drokin | 6e16818 | 2016-02-16 00:46:46 -0500 | [diff] [blame] | 3506 | if (md.lsm) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3507 | obd_free_memmd(sbi->ll_dt_exp, &md.lsm); |
| 3508 | |
| 3509 | /* refresh layout failed, need to wait */ |
| 3510 | wait_layout = rc == -EBUSY; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3511 | |
| 3512 | out: |
| 3513 | LDLM_LOCK_PUT(lock); |
| 3514 | ldlm_lock_decref(lockh, mode); |
| 3515 | |
| 3516 | /* wait for IO to complete if it's still being used. */ |
| 3517 | if (wait_layout) { |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3518 | CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n", |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 3519 | ll_get_fsname(inode->i_sb, NULL, 0), |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3520 | PFID(&lli->lli_fid), inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3521 | |
Joe Perches | ec83e61 | 2013-10-13 20:22:03 -0700 | [diff] [blame] | 3522 | memset(&conf, 0, sizeof(conf)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3523 | conf.coc_opc = OBJECT_CONF_WAIT; |
| 3524 | conf.coc_inode = inode; |
| 3525 | rc = ll_layout_conf(inode, &conf); |
| 3526 | if (rc == 0) |
| 3527 | rc = -EAGAIN; |
| 3528 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3529 | CDEBUG(D_INODE, "%s: file="DFID" waiting layout return: %d.\n", |
| 3530 | ll_get_fsname(inode->i_sb, NULL, 0), |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 3531 | PFID(&lli->lli_fid), rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3532 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3533 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | /** |
| 3537 | * This function checks if there exists a LAYOUT lock on the client side, |
| 3538 | * or enqueues it if it doesn't have one in cache. |
| 3539 | * |
| 3540 | * This function will not hold layout lock so it may be revoked any time after |
| 3541 | * this function returns. Any operations depend on layout should be redone |
| 3542 | * in that case. |
| 3543 | * |
| 3544 | * This function should be called before lov_io_init() to get an uptodate |
| 3545 | * layout version, the caller should save the version number and after IO |
| 3546 | * is finished, this function should be called again to verify that layout |
| 3547 | * is not changed during IO time. |
| 3548 | */ |
| 3549 | int ll_layout_refresh(struct inode *inode, __u32 *gen) |
| 3550 | { |
| 3551 | struct ll_inode_info *lli = ll_i2info(inode); |
| 3552 | struct ll_sb_info *sbi = ll_i2sbi(inode); |
| 3553 | struct md_op_data *op_data; |
| 3554 | struct lookup_intent it; |
| 3555 | struct lustre_handle lockh; |
Oleg Drokin | 52ee0d2 | 2016-02-24 21:59:54 -0500 | [diff] [blame] | 3556 | enum ldlm_mode mode; |
Bruce Korb | f2145ea | 2013-07-23 00:06:37 +0800 | [diff] [blame] | 3557 | struct ldlm_enqueue_info einfo = { |
| 3558 | .ei_type = LDLM_IBITS, |
| 3559 | .ei_mode = LCK_CR, |
| 3560 | .ei_cb_bl = ll_md_blocking_ast, |
| 3561 | .ei_cb_cp = ldlm_completion_ast, |
| 3562 | }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3563 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3564 | |
Jinshan Xiong | 09aed8a | 2014-04-27 13:06:50 -0400 | [diff] [blame] | 3565 | *gen = ll_layout_version_get(lli); |
| 3566 | if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != LL_LAYOUT_GEN_NONE) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3567 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3568 | |
| 3569 | /* sanity checks */ |
| 3570 | LASSERT(fid_is_sane(ll_inode2fid(inode))); |
| 3571 | LASSERT(S_ISREG(inode->i_mode)); |
| 3572 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3573 | /* take layout lock mutex to enqueue layout lock exclusively. */ |
| 3574 | mutex_lock(&lli->lli_layout_mutex); |
| 3575 | |
| 3576 | again: |
Jinshan Xiong | 09aed8a | 2014-04-27 13:06:50 -0400 | [diff] [blame] | 3577 | /* mostly layout lock is caching on the local side, so try to match |
Oleg Drokin | c0894c6 | 2016-02-24 22:00:30 -0500 | [diff] [blame] | 3578 | * it before grabbing layout lock mutex. |
| 3579 | */ |
Andrew Perepechko | 7fc1f83 | 2013-12-03 21:58:49 +0800 | [diff] [blame] | 3580 | mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0, |
| 3581 | LCK_CR | LCK_CW | LCK_PR | LCK_PW); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3582 | if (mode != 0) { /* hit cached lock */ |
| 3583 | rc = ll_layout_lock_set(&lockh, mode, inode, gen, true); |
| 3584 | if (rc == -EAGAIN) |
| 3585 | goto again; |
| 3586 | |
| 3587 | mutex_unlock(&lli->lli_layout_mutex); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3588 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, |
Oleg Drokin | e15ba45 | 2016-02-26 01:49:49 -0500 | [diff] [blame] | 3592 | 0, 0, LUSTRE_OPC_ANY, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3593 | if (IS_ERR(op_data)) { |
| 3594 | mutex_unlock(&lli->lli_layout_mutex); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3595 | return PTR_ERR(op_data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | /* have to enqueue one */ |
| 3599 | memset(&it, 0, sizeof(it)); |
| 3600 | it.it_op = IT_LAYOUT; |
| 3601 | lockh.cookie = 0ULL; |
| 3602 | |
James Nunez | 97a075c | 2016-04-27 18:21:01 -0400 | [diff] [blame] | 3603 | LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)", |
| 3604 | ll_get_fsname(inode->i_sb, NULL, 0), |
| 3605 | PFID(&lli->lli_fid), inode); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3606 | |
| 3607 | rc = md_enqueue(sbi->ll_md_exp, &einfo, &it, op_data, &lockh, |
| 3608 | NULL, 0, NULL, 0); |
John L. Hammond | 8bf86fd | 2016-06-20 16:55:40 -0400 | [diff] [blame] | 3609 | ptlrpc_req_finished(it.it_request); |
| 3610 | it.it_request = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3611 | |
| 3612 | ll_finish_md_op_data(op_data); |
| 3613 | |
John L. Hammond | e476f2e | 2016-06-20 16:55:38 -0400 | [diff] [blame] | 3614 | mode = it.it_lock_mode; |
| 3615 | it.it_lock_mode = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3616 | ll_intent_drop_lock(&it); |
| 3617 | |
| 3618 | if (rc == 0) { |
| 3619 | /* set lock data in case this is a new lock */ |
| 3620 | ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL); |
| 3621 | rc = ll_layout_lock_set(&lockh, mode, inode, gen, true); |
| 3622 | if (rc == -EAGAIN) |
| 3623 | goto again; |
| 3624 | } |
| 3625 | mutex_unlock(&lli->lli_layout_mutex); |
| 3626 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3627 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3628 | } |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3629 | |
| 3630 | /** |
| 3631 | * This function send a restore request to the MDT |
| 3632 | */ |
Jinshan Xiong | 1b1594d | 2016-04-12 16:14:10 -0400 | [diff] [blame] | 3633 | int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length) |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3634 | { |
| 3635 | struct hsm_user_request *hur; |
| 3636 | int len, rc; |
| 3637 | |
| 3638 | len = sizeof(struct hsm_user_request) + |
| 3639 | sizeof(struct hsm_user_item); |
Julia Lawall | 496a51b | 2014-09-18 22:24:02 +0200 | [diff] [blame] | 3640 | hur = kzalloc(len, GFP_NOFS); |
| 3641 | if (!hur) |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3642 | return -ENOMEM; |
| 3643 | |
| 3644 | hur->hur_request.hr_action = HUA_RESTORE; |
| 3645 | hur->hur_request.hr_archive_id = 0; |
| 3646 | hur->hur_request.hr_flags = 0; |
| 3647 | memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid, |
| 3648 | sizeof(hur->hur_user_item[0].hui_fid)); |
Jinshan Xiong | 1b1594d | 2016-04-12 16:14:10 -0400 | [diff] [blame] | 3649 | hur->hur_user_item[0].hui_extent.offset = offset; |
| 3650 | hur->hur_user_item[0].hui_extent.length = length; |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3651 | hur->hur_request.hr_itemcount = 1; |
John L. Hammond | 1929c43 | 2016-03-30 19:48:37 -0400 | [diff] [blame] | 3652 | rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp, |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3653 | len, hur, NULL); |
Julia Lawall | 97903a2 | 2015-04-12 22:55:02 +0200 | [diff] [blame] | 3654 | kfree(hur); |
JC Lafoucriere | 5ea17d6 | 2013-11-21 22:24:48 +0800 | [diff] [blame] | 3655 | return rc; |
| 3656 | } |