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