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