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