Whamcloud - gitweb
LU-10681: Disable tiny writes for append
[fs/lustre-release.git] / lustre / llite / file.c
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
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
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  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
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
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #ifdef HAVE_UIDGID_HEADER
46 # include <linux/uidgid.h>
47 #endif
48
49 #include <uapi/linux/lustre/lustre_ioctl.h>
50 #include <lustre_swab.h>
51
52 #include "cl_object.h"
53 #include "llite_internal.h"
54 #include "vvp_internal.h"
55
56 struct split_param {
57         struct inode    *sp_inode;
58         __u16           sp_mirror_id;
59 };
60
61 static int
62 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
63
64 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
65                           bool *lease_broken);
66
67 static struct ll_file_data *ll_file_data_get(void)
68 {
69         struct ll_file_data *fd;
70
71         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
72         if (fd == NULL)
73                 return NULL;
74
75         fd->fd_write_failed = false;
76
77         return fd;
78 }
79
80 static void ll_file_data_put(struct ll_file_data *fd)
81 {
82         if (fd != NULL)
83                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
84 }
85
86 /**
87  * Packs all the attributes into @op_data for the CLOSE rpc.
88  */
89 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
90                              struct obd_client_handle *och)
91 {
92         ENTRY;
93
94         ll_prep_md_op_data(op_data, inode, NULL, NULL,
95                            0, 0, LUSTRE_OPC_ANY, NULL);
96
97         op_data->op_attr.ia_mode = inode->i_mode;
98         op_data->op_attr.ia_atime = inode->i_atime;
99         op_data->op_attr.ia_mtime = inode->i_mtime;
100         op_data->op_attr.ia_ctime = inode->i_ctime;
101         op_data->op_attr.ia_size = i_size_read(inode);
102         op_data->op_attr.ia_valid |= ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
103                                      ATTR_MTIME | ATTR_MTIME_SET |
104                                      ATTR_CTIME | ATTR_CTIME_SET;
105         op_data->op_attr_blocks = inode->i_blocks;
106         op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
107         op_data->op_handle = och->och_fh;
108
109         if (och->och_flags & FMODE_WRITE &&
110             ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
111                 /* For HSM: if inode data has been modified, pack it so that
112                  * MDT can set data dirty flag in the archive. */
113                 op_data->op_bias |= MDS_DATA_MODIFIED;
114
115         EXIT;
116 }
117
118 /**
119  * Perform a close, possibly with a bias.
120  * The meaning of "data" depends on the value of "bias".
121  *
122  * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
123  * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
124  * swap layouts with.
125  */
126 static int ll_close_inode_openhandle(struct inode *inode,
127                                      struct obd_client_handle *och,
128                                      enum mds_op_bias bias, void *data)
129 {
130         struct obd_export *md_exp = ll_i2mdexp(inode);
131         const struct ll_inode_info *lli = ll_i2info(inode);
132         struct md_op_data *op_data;
133         struct ptlrpc_request *req = NULL;
134         int rc;
135         ENTRY;
136
137         if (class_exp2obd(md_exp) == NULL) {
138                 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
139                        ll_get_fsname(inode->i_sb, NULL, 0),
140                        PFID(&lli->lli_fid));
141                 GOTO(out, rc = 0);
142         }
143
144         OBD_ALLOC_PTR(op_data);
145         /* We leak openhandle and request here on error, but not much to be
146          * done in OOM case since app won't retry close on error either. */
147         if (op_data == NULL)
148                 GOTO(out, rc = -ENOMEM);
149
150         ll_prepare_close(inode, op_data, och);
151         switch (bias) {
152         case MDS_CLOSE_LAYOUT_MERGE:
153                 /* merge blocks from the victim inode */
154                 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
155                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
156         case MDS_CLOSE_LAYOUT_SPLIT:
157         case MDS_CLOSE_LAYOUT_SWAP: {
158                 struct split_param *sp = data;
159
160                 LASSERT(data != NULL);
161                 op_data->op_bias |= bias;
162                 op_data->op_data_version = 0;
163                 op_data->op_lease_handle = och->och_lease_handle;
164                 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
165                         op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
166                         op_data->op_mirror_id = sp->sp_mirror_id;
167                 } else {
168                         op_data->op_fid2 = *ll_inode2fid(data);
169                 }
170                 break;
171         }
172
173         case MDS_CLOSE_RESYNC_DONE: {
174                 struct ll_ioc_lease *ioc = data;
175
176                 LASSERT(data != NULL);
177                 op_data->op_attr_blocks +=
178                         ioc->lil_count * op_data->op_attr_blocks;
179                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
180                 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
181
182                 op_data->op_lease_handle = och->och_lease_handle;
183                 op_data->op_data = &ioc->lil_ids[0];
184                 op_data->op_data_size =
185                         ioc->lil_count * sizeof(ioc->lil_ids[0]);
186                 break;
187         }
188
189         case MDS_HSM_RELEASE:
190                 LASSERT(data != NULL);
191                 op_data->op_bias |= MDS_HSM_RELEASE;
192                 op_data->op_data_version = *(__u64 *)data;
193                 op_data->op_lease_handle = och->och_lease_handle;
194                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
195                 break;
196
197         default:
198                 LASSERT(data == NULL);
199                 break;
200         }
201
202         rc = md_close(md_exp, op_data, och->och_mod, &req);
203         if (rc != 0 && rc != -EINTR)
204                 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
205                        md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
206
207         if (rc == 0 && op_data->op_bias & bias) {
208                 struct mdt_body *body;
209
210                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
211                 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
212                         rc = -EBUSY;
213         }
214
215         ll_finish_md_op_data(op_data);
216         EXIT;
217 out:
218
219         md_clear_open_replay_data(md_exp, och);
220         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
221         OBD_FREE_PTR(och);
222
223         ptlrpc_req_finished(req);       /* This is close request */
224         return rc;
225 }
226
227 int ll_md_real_close(struct inode *inode, fmode_t fmode)
228 {
229         struct ll_inode_info *lli = ll_i2info(inode);
230         struct obd_client_handle **och_p;
231         struct obd_client_handle *och;
232         __u64 *och_usecount;
233         int rc = 0;
234         ENTRY;
235
236         if (fmode & FMODE_WRITE) {
237                 och_p = &lli->lli_mds_write_och;
238                 och_usecount = &lli->lli_open_fd_write_count;
239         } else if (fmode & FMODE_EXEC) {
240                 och_p = &lli->lli_mds_exec_och;
241                 och_usecount = &lli->lli_open_fd_exec_count;
242         } else {
243                 LASSERT(fmode & FMODE_READ);
244                 och_p = &lli->lli_mds_read_och;
245                 och_usecount = &lli->lli_open_fd_read_count;
246         }
247
248         mutex_lock(&lli->lli_och_mutex);
249         if (*och_usecount > 0) {
250                 /* There are still users of this handle, so skip
251                  * freeing it. */
252                 mutex_unlock(&lli->lli_och_mutex);
253                 RETURN(0);
254         }
255
256         och = *och_p;
257         *och_p = NULL;
258         mutex_unlock(&lli->lli_och_mutex);
259
260         if (och != NULL) {
261                 /* There might be a race and this handle may already
262                  * be closed. */
263                 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
264         }
265
266         RETURN(rc);
267 }
268
269 static int ll_md_close(struct inode *inode, struct file *file)
270 {
271         union ldlm_policy_data policy = {
272                 .l_inodebits    = { MDS_INODELOCK_OPEN },
273         };
274         __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
275         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
276         struct ll_inode_info *lli = ll_i2info(inode);
277         struct lustre_handle lockh;
278         enum ldlm_mode lockmode;
279         int rc = 0;
280         ENTRY;
281
282         /* clear group lock, if present */
283         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
284                 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
285
286         if (fd->fd_lease_och != NULL) {
287                 bool lease_broken;
288
289                 /* Usually the lease is not released when the
290                  * application crashed, we need to release here. */
291                 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
292                 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
293                         PFID(&lli->lli_fid), rc, lease_broken);
294
295                 fd->fd_lease_och = NULL;
296         }
297
298         if (fd->fd_och != NULL) {
299                 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
300                 fd->fd_och = NULL;
301                 GOTO(out, rc);
302         }
303
304         /* Let's see if we have good enough OPEN lock on the file and if
305            we can skip talking to MDS */
306         mutex_lock(&lli->lli_och_mutex);
307         if (fd->fd_omode & FMODE_WRITE) {
308                 lockmode = LCK_CW;
309                 LASSERT(lli->lli_open_fd_write_count);
310                 lli->lli_open_fd_write_count--;
311         } else if (fd->fd_omode & FMODE_EXEC) {
312                 lockmode = LCK_PR;
313                 LASSERT(lli->lli_open_fd_exec_count);
314                 lli->lli_open_fd_exec_count--;
315         } else {
316                 lockmode = LCK_CR;
317                 LASSERT(lli->lli_open_fd_read_count);
318                 lli->lli_open_fd_read_count--;
319         }
320         mutex_unlock(&lli->lli_och_mutex);
321
322         if (!md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
323                            LDLM_IBITS, &policy, lockmode, &lockh))
324                 rc = ll_md_real_close(inode, fd->fd_omode);
325
326 out:
327         LUSTRE_FPRIVATE(file) = NULL;
328         ll_file_data_put(fd);
329
330         RETURN(rc);
331 }
332
333 /* While this returns an error code, fput() the caller does not, so we need
334  * to make every effort to clean up all of our state here.  Also, applications
335  * rarely check close errors and even if an error is returned they will not
336  * re-try the close call.
337  */
338 int ll_file_release(struct inode *inode, struct file *file)
339 {
340         struct ll_file_data *fd;
341         struct ll_sb_info *sbi = ll_i2sbi(inode);
342         struct ll_inode_info *lli = ll_i2info(inode);
343         int rc;
344         ENTRY;
345
346         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
347                PFID(ll_inode2fid(inode)), inode);
348
349         if (inode->i_sb->s_root != file_dentry(file))
350                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
351         fd = LUSTRE_FPRIVATE(file);
352         LASSERT(fd != NULL);
353
354         /* The last ref on @file, maybe not the the owner pid of statahead,
355          * because parent and child process can share the same file handle. */
356         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
357                 ll_deauthorize_statahead(inode, fd);
358
359         if (inode->i_sb->s_root == file_dentry(file)) {
360                 LUSTRE_FPRIVATE(file) = NULL;
361                 ll_file_data_put(fd);
362                 RETURN(0);
363         }
364
365         if (!S_ISDIR(inode->i_mode)) {
366                 if (lli->lli_clob != NULL)
367                         lov_read_and_clear_async_rc(lli->lli_clob);
368                 lli->lli_async_rc = 0;
369         }
370
371         rc = ll_md_close(inode, file);
372
373         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
374                 libcfs_debug_dumplog();
375
376         RETURN(rc);
377 }
378
379 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
380                                 struct lookup_intent *itp)
381 {
382         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
383         struct dentry *parent = de->d_parent;
384         const char *name = NULL;
385         int len = 0;
386         struct md_op_data *op_data;
387         struct ptlrpc_request *req = NULL;
388         int rc;
389         ENTRY;
390
391         LASSERT(parent != NULL);
392         LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
393
394         /* if server supports open-by-fid, or file name is invalid, don't pack
395          * name in open request */
396         if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID) &&
397             lu_name_is_valid_2(de->d_name.name, de->d_name.len)) {
398                 name = de->d_name.name;
399                 len = de->d_name.len;
400         }
401
402         op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
403                                      name, len, 0, LUSTRE_OPC_ANY, NULL);
404         if (IS_ERR(op_data))
405                 RETURN(PTR_ERR(op_data));
406         op_data->op_data = lmm;
407         op_data->op_data_size = lmmsize;
408
409         rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
410                             &ll_md_blocking_ast, 0);
411         ll_finish_md_op_data(op_data);
412         if (rc == -ESTALE) {
413                 /* reason for keep own exit path - don`t flood log
414                  * with messages with -ESTALE errors.
415                  */
416                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
417                      it_open_error(DISP_OPEN_OPEN, itp))
418                         GOTO(out, rc);
419                 ll_release_openhandle(de, itp);
420                 GOTO(out, rc);
421         }
422
423         if (it_disposition(itp, DISP_LOOKUP_NEG))
424                 GOTO(out, rc = -ENOENT);
425
426         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
427                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
428                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
429                 GOTO(out, rc);
430         }
431
432         rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
433         if (!rc && itp->it_lock_mode)
434                 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
435
436 out:
437         ptlrpc_req_finished(req);
438         ll_intent_drop_lock(itp);
439
440         /* We did open by fid, but by the time we got to the server,
441          * the object disappeared. If this is a create, we cannot really
442          * tell the userspace that the file it was trying to create
443          * does not exist. Instead let's return -ESTALE, and the VFS will
444          * retry the create with LOOKUP_REVAL that we are going to catch
445          * in ll_revalidate_dentry() and use lookup then.
446          */
447         if (rc == -ENOENT && itp->it_op & IT_CREAT)
448                 rc = -ESTALE;
449
450         RETURN(rc);
451 }
452
453 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
454                        struct obd_client_handle *och)
455 {
456         struct mdt_body *body;
457
458         body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
459         och->och_fh = body->mbo_handle;
460         och->och_fid = body->mbo_fid1;
461         och->och_lease_handle.cookie = it->it_lock_handle;
462         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
463         och->och_flags = it->it_flags;
464
465         return md_set_open_replay_data(md_exp, och, it);
466 }
467
468 static int ll_local_open(struct file *file, struct lookup_intent *it,
469                          struct ll_file_data *fd, struct obd_client_handle *och)
470 {
471         struct inode *inode = file_inode(file);
472         ENTRY;
473
474         LASSERT(!LUSTRE_FPRIVATE(file));
475
476         LASSERT(fd != NULL);
477
478         if (och) {
479                 int rc;
480
481                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
482                 if (rc != 0)
483                         RETURN(rc);
484         }
485
486         LUSTRE_FPRIVATE(file) = fd;
487         ll_readahead_init(inode, &fd->fd_ras);
488         fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
489
490         /* ll_cl_context initialize */
491         rwlock_init(&fd->fd_lock);
492         INIT_LIST_HEAD(&fd->fd_lccs);
493
494         RETURN(0);
495 }
496
497 /* Open a file, and (for the very first open) create objects on the OSTs at
498  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
499  * creation or open until ll_lov_setstripe() ioctl is called.
500  *
501  * If we already have the stripe MD locally then we don't request it in
502  * md_open(), by passing a lmm_size = 0.
503  *
504  * It is up to the application to ensure no other processes open this file
505  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
506  * used.  We might be able to avoid races of that sort by getting lli_open_sem
507  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
508  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
509  */
510 int ll_file_open(struct inode *inode, struct file *file)
511 {
512         struct ll_inode_info *lli = ll_i2info(inode);
513         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
514                                           .it_flags = file->f_flags };
515         struct obd_client_handle **och_p = NULL;
516         __u64 *och_usecount = NULL;
517         struct ll_file_data *fd;
518         int rc = 0;
519         ENTRY;
520
521         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
522                PFID(ll_inode2fid(inode)), inode, file->f_flags);
523
524         it = file->private_data; /* XXX: compat macro */
525         file->private_data = NULL; /* prevent ll_local_open assertion */
526
527         fd = ll_file_data_get();
528         if (fd == NULL)
529                 GOTO(out_nofiledata, rc = -ENOMEM);
530
531         fd->fd_file = file;
532         if (S_ISDIR(inode->i_mode))
533                 ll_authorize_statahead(inode, fd);
534
535         if (inode->i_sb->s_root == file_dentry(file)) {
536                 LUSTRE_FPRIVATE(file) = fd;
537                 RETURN(0);
538         }
539
540         if (!it || !it->it_disposition) {
541                 /* Convert f_flags into access mode. We cannot use file->f_mode,
542                  * because everything but O_ACCMODE mask was stripped from
543                  * there */
544                 if ((oit.it_flags + 1) & O_ACCMODE)
545                         oit.it_flags++;
546                 if (file->f_flags & O_TRUNC)
547                         oit.it_flags |= FMODE_WRITE;
548
549                 /* kernel only call f_op->open in dentry_open.  filp_open calls
550                  * dentry_open after call to open_namei that checks permissions.
551                  * Only nfsd_open call dentry_open directly without checking
552                  * permissions and because of that this code below is safe. */
553                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
554                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
555
556                 /* We do not want O_EXCL here, presumably we opened the file
557                  * already? XXX - NFS implications? */
558                 oit.it_flags &= ~O_EXCL;
559
560                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
561                  * created if necessary, then "IT_CREAT" should be set to keep
562                  * consistent with it */
563                 if (oit.it_flags & O_CREAT)
564                         oit.it_op |= IT_CREAT;
565
566                 it = &oit;
567         }
568
569 restart:
570         /* Let's see if we have file open on MDS already. */
571         if (it->it_flags & FMODE_WRITE) {
572                 och_p = &lli->lli_mds_write_och;
573                 och_usecount = &lli->lli_open_fd_write_count;
574         } else if (it->it_flags & FMODE_EXEC) {
575                 och_p = &lli->lli_mds_exec_och;
576                 och_usecount = &lli->lli_open_fd_exec_count;
577          } else {
578                 och_p = &lli->lli_mds_read_och;
579                 och_usecount = &lli->lli_open_fd_read_count;
580         }
581
582         mutex_lock(&lli->lli_och_mutex);
583         if (*och_p) { /* Open handle is present */
584                 if (it_disposition(it, DISP_OPEN_OPEN)) {
585                         /* Well, there's extra open request that we do not need,
586                            let's close it somehow. This will decref request. */
587                         rc = it_open_error(DISP_OPEN_OPEN, it);
588                         if (rc) {
589                                 mutex_unlock(&lli->lli_och_mutex);
590                                 GOTO(out_openerr, rc);
591                         }
592
593                         ll_release_openhandle(file_dentry(file), it);
594                 }
595                 (*och_usecount)++;
596
597                 rc = ll_local_open(file, it, fd, NULL);
598                 if (rc) {
599                         (*och_usecount)--;
600                         mutex_unlock(&lli->lli_och_mutex);
601                         GOTO(out_openerr, rc);
602                 }
603         } else {
604                 LASSERT(*och_usecount == 0);
605                 if (!it->it_disposition) {
606                         struct ll_dentry_data *ldd = ll_d2d(file->f_path.dentry);
607                         /* We cannot just request lock handle now, new ELC code
608                            means that one of other OPEN locks for this file
609                            could be cancelled, and since blocking ast handler
610                            would attempt to grab och_mutex as well, that would
611                            result in a deadlock */
612                         mutex_unlock(&lli->lli_och_mutex);
613                         /*
614                          * Normally called under two situations:
615                          * 1. NFS export.
616                          * 2. A race/condition on MDS resulting in no open
617                          *    handle to be returned from LOOKUP|OPEN request,
618                          *    for example if the target entry was a symlink.
619                          *
620                          *  Only fetch MDS_OPEN_LOCK if this is in NFS path,
621                          *  marked by a bit set in ll_iget_for_nfs. Clear the
622                          *  bit so that it's not confusing later callers.
623                          *
624                          *  NB; when ldd is NULL, it must have come via normal
625                          *  lookup path only, since ll_iget_for_nfs always calls
626                          *  ll_d_init().
627                          */
628                         if (ldd && ldd->lld_nfs_dentry) {
629                                 ldd->lld_nfs_dentry = 0;
630                                 it->it_flags |= MDS_OPEN_LOCK;
631                         }
632
633                          /*
634                          * Always specify MDS_OPEN_BY_FID because we don't want
635                          * to get file with different fid.
636                          */
637                         it->it_flags |= MDS_OPEN_BY_FID;
638                         rc = ll_intent_file_open(file_dentry(file), NULL, 0,
639                                                  it);
640                         if (rc)
641                                 GOTO(out_openerr, rc);
642
643                         goto restart;
644                 }
645                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
646                 if (!*och_p)
647                         GOTO(out_och_free, rc = -ENOMEM);
648
649                 (*och_usecount)++;
650
651                 /* md_intent_lock() didn't get a request ref if there was an
652                  * open error, so don't do cleanup on the request here
653                  * (bug 3430) */
654                 /* XXX (green): Should not we bail out on any error here, not
655                  * just open error? */
656                 rc = it_open_error(DISP_OPEN_OPEN, it);
657                 if (rc != 0)
658                         GOTO(out_och_free, rc);
659
660                 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
661                          "inode %p: disposition %x, status %d\n", inode,
662                          it_disposition(it, ~0), it->it_status);
663
664                 rc = ll_local_open(file, it, fd, *och_p);
665                 if (rc)
666                         GOTO(out_och_free, rc);
667         }
668         mutex_unlock(&lli->lli_och_mutex);
669         fd = NULL;
670
671         /* Must do this outside lli_och_mutex lock to prevent deadlock where
672            different kind of OPEN lock for this same inode gets cancelled
673            by ldlm_cancel_lru */
674         if (!S_ISREG(inode->i_mode))
675                 GOTO(out_och_free, rc);
676
677         cl_lov_delay_create_clear(&file->f_flags);
678         GOTO(out_och_free, rc);
679
680 out_och_free:
681         if (rc) {
682                 if (och_p && *och_p) {
683                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
684                         *och_p = NULL; /* OBD_FREE writes some magic there */
685                         (*och_usecount)--;
686                 }
687                 mutex_unlock(&lli->lli_och_mutex);
688
689 out_openerr:
690                 if (lli->lli_opendir_key == fd)
691                         ll_deauthorize_statahead(inode, fd);
692                 if (fd != NULL)
693                         ll_file_data_put(fd);
694         } else {
695                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
696         }
697
698 out_nofiledata:
699         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
700                 ptlrpc_req_finished(it->it_request);
701                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
702         }
703
704         return rc;
705 }
706
707 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
708                         struct ldlm_lock_desc *desc, void *data, int flag)
709 {
710         int rc;
711         struct lustre_handle lockh;
712         ENTRY;
713
714         switch (flag) {
715         case LDLM_CB_BLOCKING:
716                 ldlm_lock2handle(lock, &lockh);
717                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
718                 if (rc < 0) {
719                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
720                         RETURN(rc);
721                 }
722                 break;
723         case LDLM_CB_CANCELING:
724                 /* do nothing */
725                 break;
726         }
727         RETURN(0);
728 }
729
730 /**
731  * When setting a lease on a file, we take ownership of the lli_mds_*_och
732  * and save it as fd->fd_och so as to force client to reopen the file even
733  * if it has an open lock in cache already.
734  */
735 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
736                                 struct lustre_handle *old_handle)
737 {
738         struct ll_inode_info *lli = ll_i2info(inode);
739         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
740         struct obd_client_handle **och_p;
741         __u64 *och_usecount;
742         int rc = 0;
743         ENTRY;
744
745         /* Get the openhandle of the file */
746         mutex_lock(&lli->lli_och_mutex);
747         if (fd->fd_lease_och != NULL)
748                 GOTO(out_unlock, rc = -EBUSY);
749
750         if (fd->fd_och == NULL) {
751                 if (file->f_mode & FMODE_WRITE) {
752                         LASSERT(lli->lli_mds_write_och != NULL);
753                         och_p = &lli->lli_mds_write_och;
754                         och_usecount = &lli->lli_open_fd_write_count;
755                 } else {
756                         LASSERT(lli->lli_mds_read_och != NULL);
757                         och_p = &lli->lli_mds_read_och;
758                         och_usecount = &lli->lli_open_fd_read_count;
759                 }
760
761                 if (*och_usecount > 1)
762                         GOTO(out_unlock, rc = -EBUSY);
763
764                 fd->fd_och = *och_p;
765                 *och_usecount = 0;
766                 *och_p = NULL;
767         }
768
769         *old_handle = fd->fd_och->och_fh;
770
771         EXIT;
772 out_unlock:
773         mutex_unlock(&lli->lli_och_mutex);
774         return rc;
775 }
776
777 /**
778  * Release ownership on lli_mds_*_och when putting back a file lease.
779  */
780 static int ll_lease_och_release(struct inode *inode, struct file *file)
781 {
782         struct ll_inode_info *lli = ll_i2info(inode);
783         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
784         struct obd_client_handle **och_p;
785         struct obd_client_handle *old_och = NULL;
786         __u64 *och_usecount;
787         int rc = 0;
788         ENTRY;
789
790         mutex_lock(&lli->lli_och_mutex);
791         if (file->f_mode & FMODE_WRITE) {
792                 och_p = &lli->lli_mds_write_och;
793                 och_usecount = &lli->lli_open_fd_write_count;
794         } else {
795                 och_p = &lli->lli_mds_read_och;
796                 och_usecount = &lli->lli_open_fd_read_count;
797         }
798
799         /* The file may have been open by another process (broken lease) so
800          * *och_p is not NULL. In this case we should simply increase usecount
801          * and close fd_och.
802          */
803         if (*och_p != NULL) {
804                 old_och = fd->fd_och;
805                 (*och_usecount)++;
806         } else {
807                 *och_p = fd->fd_och;
808                 *och_usecount = 1;
809         }
810         fd->fd_och = NULL;
811         mutex_unlock(&lli->lli_och_mutex);
812
813         if (old_och != NULL)
814                 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
815
816         RETURN(rc);
817 }
818
819 /**
820  * Acquire a lease and open the file.
821  */
822 static struct obd_client_handle *
823 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
824               __u64 open_flags)
825 {
826         struct lookup_intent it = { .it_op = IT_OPEN };
827         struct ll_sb_info *sbi = ll_i2sbi(inode);
828         struct md_op_data *op_data;
829         struct ptlrpc_request *req = NULL;
830         struct lustre_handle old_handle = { 0 };
831         struct obd_client_handle *och = NULL;
832         int rc;
833         int rc2;
834         ENTRY;
835
836         if (fmode != FMODE_WRITE && fmode != FMODE_READ)
837                 RETURN(ERR_PTR(-EINVAL));
838
839         if (file != NULL) {
840                 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
841                         RETURN(ERR_PTR(-EPERM));
842
843                 rc = ll_lease_och_acquire(inode, file, &old_handle);
844                 if (rc)
845                         RETURN(ERR_PTR(rc));
846         }
847
848         OBD_ALLOC_PTR(och);
849         if (och == NULL)
850                 RETURN(ERR_PTR(-ENOMEM));
851
852         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
853                                         LUSTRE_OPC_ANY, NULL);
854         if (IS_ERR(op_data))
855                 GOTO(out, rc = PTR_ERR(op_data));
856
857         /* To tell the MDT this openhandle is from the same owner */
858         op_data->op_handle = old_handle;
859
860         it.it_flags = fmode | open_flags;
861         it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
862         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
863                             &ll_md_blocking_lease_ast,
864         /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
865          * it can be cancelled which may mislead applications that the lease is
866          * broken;
867          * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
868          * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
869          * doesn't deal with openhandle, so normal openhandle will be leaked. */
870                             LDLM_FL_NO_LRU | LDLM_FL_EXCL);
871         ll_finish_md_op_data(op_data);
872         ptlrpc_req_finished(req);
873         if (rc < 0)
874                 GOTO(out_release_it, rc);
875
876         if (it_disposition(&it, DISP_LOOKUP_NEG))
877                 GOTO(out_release_it, rc = -ENOENT);
878
879         rc = it_open_error(DISP_OPEN_OPEN, &it);
880         if (rc)
881                 GOTO(out_release_it, rc);
882
883         LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
884         ll_och_fill(sbi->ll_md_exp, &it, och);
885
886         if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
887                 GOTO(out_close, rc = -EOPNOTSUPP);
888
889         /* already get lease, handle lease lock */
890         ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
891         if (it.it_lock_mode == 0 ||
892             it.it_lock_bits != MDS_INODELOCK_OPEN) {
893                 /* open lock must return for lease */
894                 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
895                         PFID(ll_inode2fid(inode)), it.it_lock_mode,
896                         it.it_lock_bits);
897                 GOTO(out_close, rc = -EPROTO);
898         }
899
900         ll_intent_release(&it);
901         RETURN(och);
902
903 out_close:
904         /* Cancel open lock */
905         if (it.it_lock_mode != 0) {
906                 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
907                                             it.it_lock_mode);
908                 it.it_lock_mode = 0;
909                 och->och_lease_handle.cookie = 0ULL;
910         }
911         rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
912         if (rc2 < 0)
913                 CERROR("%s: error closing file "DFID": %d\n",
914                        ll_get_fsname(inode->i_sb, NULL, 0),
915                        PFID(&ll_i2info(inode)->lli_fid), rc2);
916         och = NULL; /* och has been freed in ll_close_inode_openhandle() */
917 out_release_it:
918         ll_intent_release(&it);
919 out:
920         if (och != NULL)
921                 OBD_FREE_PTR(och);
922         RETURN(ERR_PTR(rc));
923 }
924
925 /**
926  * Check whether a layout swap can be done between two inodes.
927  *
928  * \param[in] inode1  First inode to check
929  * \param[in] inode2  Second inode to check
930  *
931  * \retval 0 on success, layout swap can be performed between both inodes
932  * \retval negative error code if requirements are not met
933  */
934 static int ll_check_swap_layouts_validity(struct inode *inode1,
935                                           struct inode *inode2)
936 {
937         if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
938                 return -EINVAL;
939
940         if (inode_permission(inode1, MAY_WRITE) ||
941             inode_permission(inode2, MAY_WRITE))
942                 return -EPERM;
943
944         if (inode1->i_sb != inode2->i_sb)
945                 return -EXDEV;
946
947         return 0;
948 }
949
950 static int ll_swap_layouts_close(struct obd_client_handle *och,
951                                  struct inode *inode, struct inode *inode2)
952 {
953         const struct lu_fid     *fid1 = ll_inode2fid(inode);
954         const struct lu_fid     *fid2;
955         int                      rc;
956         ENTRY;
957
958         CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
959                ll_get_fsname(inode->i_sb, NULL, 0), PFID(fid1));
960
961         rc = ll_check_swap_layouts_validity(inode, inode2);
962         if (rc < 0)
963                 GOTO(out_free_och, rc);
964
965         /* We now know that inode2 is a lustre inode */
966         fid2 = ll_inode2fid(inode2);
967
968         rc = lu_fid_cmp(fid1, fid2);
969         if (rc == 0)
970                 GOTO(out_free_och, rc = -EINVAL);
971
972         /* Close the file and {swap,merge} layouts between inode & inode2.
973          * NB: lease lock handle is released in mdc_close_layout_swap_pack()
974          * because we still need it to pack l_remote_handle to MDT. */
975         rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
976                                        inode2);
977
978         och = NULL; /* freed in ll_close_inode_openhandle() */
979
980 out_free_och:
981         if (och != NULL)
982                 OBD_FREE_PTR(och);
983
984         RETURN(rc);
985 }
986
987 /**
988  * Release lease and close the file.
989  * It will check if the lease has ever broken.
990  */
991 static int ll_lease_close_intent(struct obd_client_handle *och,
992                                  struct inode *inode,
993                                  bool *lease_broken, enum mds_op_bias bias,
994                                  void *data)
995 {
996         struct ldlm_lock *lock;
997         bool cancelled = true;
998         int rc;
999         ENTRY;
1000
1001         lock = ldlm_handle2lock(&och->och_lease_handle);
1002         if (lock != NULL) {
1003                 lock_res_and_lock(lock);
1004                 cancelled = ldlm_is_cancel(lock);
1005                 unlock_res_and_lock(lock);
1006                 LDLM_LOCK_PUT(lock);
1007         }
1008
1009         CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1010                PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1011
1012         if (lease_broken != NULL)
1013                 *lease_broken = cancelled;
1014
1015         if (!cancelled && !bias)
1016                 ldlm_cli_cancel(&och->och_lease_handle, 0);
1017
1018         if (cancelled) { /* no need to excute intent */
1019                 bias = 0;
1020                 data = NULL;
1021         }
1022
1023         rc = ll_close_inode_openhandle(inode, och, bias, data);
1024         RETURN(rc);
1025 }
1026
1027 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1028                           bool *lease_broken)
1029 {
1030         return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1031 }
1032
1033 /**
1034  * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1035  */
1036 static int ll_lease_file_resync(struct obd_client_handle *och,
1037                                 struct inode *inode)
1038 {
1039         struct ll_sb_info *sbi = ll_i2sbi(inode);
1040         struct md_op_data *op_data;
1041         __u64 data_version_unused;
1042         int rc;
1043         ENTRY;
1044
1045         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1046                                      LUSTRE_OPC_ANY, NULL);
1047         if (IS_ERR(op_data))
1048                 RETURN(PTR_ERR(op_data));
1049
1050         /* before starting file resync, it's necessary to clean up page cache
1051          * in client memory, otherwise once the layout version is increased,
1052          * writing back cached data will be denied the OSTs. */
1053         rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1054         if (rc)
1055                 GOTO(out, rc);
1056
1057         op_data->op_handle = och->och_lease_handle;
1058         rc = md_file_resync(sbi->ll_md_exp, op_data);
1059         if (rc)
1060                 GOTO(out, rc);
1061
1062         EXIT;
1063 out:
1064         ll_finish_md_op_data(op_data);
1065         return rc;
1066 }
1067
1068 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1069 {
1070         struct ll_inode_info *lli = ll_i2info(inode);
1071         struct cl_object *obj = lli->lli_clob;
1072         struct cl_attr *attr = vvp_env_thread_attr(env);
1073         s64 atime;
1074         s64 mtime;
1075         s64 ctime;
1076         int rc = 0;
1077
1078         ENTRY;
1079
1080         ll_inode_size_lock(inode);
1081
1082         /* Merge timestamps the most recently obtained from MDS with
1083          * timestamps obtained from OSTs.
1084          *
1085          * Do not overwrite atime of inode because it may be refreshed
1086          * by file_accessed() function. If the read was served by cache
1087          * data, there is no RPC to be sent so that atime may not be
1088          * transferred to OSTs at all. MDT only updates atime at close time
1089          * if it's at least 'mdd.*.atime_diff' older.
1090          * All in all, the atime in Lustre does not strictly comply with
1091          * POSIX. Solving this problem needs to send an RPC to MDT for each
1092          * read, this will hurt performance. */
1093         if (LTIME_S(inode->i_atime) < lli->lli_atime || lli->lli_update_atime) {
1094                 LTIME_S(inode->i_atime) = lli->lli_atime;
1095                 lli->lli_update_atime = 0;
1096         }
1097         LTIME_S(inode->i_mtime) = lli->lli_mtime;
1098         LTIME_S(inode->i_ctime) = lli->lli_ctime;
1099
1100         atime = LTIME_S(inode->i_atime);
1101         mtime = LTIME_S(inode->i_mtime);
1102         ctime = LTIME_S(inode->i_ctime);
1103
1104         cl_object_attr_lock(obj);
1105         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1106                 rc = -EINVAL;
1107         else
1108                 rc = cl_object_attr_get(env, obj, attr);
1109         cl_object_attr_unlock(obj);
1110
1111         if (rc != 0)
1112                 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1113
1114         if (atime < attr->cat_atime)
1115                 atime = attr->cat_atime;
1116
1117         if (ctime < attr->cat_ctime)
1118                 ctime = attr->cat_ctime;
1119
1120         if (mtime < attr->cat_mtime)
1121                 mtime = attr->cat_mtime;
1122
1123         CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1124                PFID(&lli->lli_fid), attr->cat_size);
1125
1126         i_size_write(inode, attr->cat_size);
1127         inode->i_blocks = attr->cat_blocks;
1128
1129         LTIME_S(inode->i_atime) = atime;
1130         LTIME_S(inode->i_mtime) = mtime;
1131         LTIME_S(inode->i_ctime) = ctime;
1132
1133 out_size_unlock:
1134         ll_inode_size_unlock(inode);
1135
1136         RETURN(rc);
1137 }
1138
1139 /**
1140  * Set designated mirror for I/O.
1141  *
1142  * So far only read, write, and truncated can support to issue I/O to
1143  * designated mirror.
1144  */
1145 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1146 {
1147         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1148
1149         /* clear layout version for generic(non-resync) I/O in case it carries
1150          * stale layout version due to I/O restart */
1151         io->ci_layout_version = 0;
1152
1153         /* FLR: disable non-delay for designated mirror I/O because obviously
1154          * only one mirror is available */
1155         if (fd->fd_designated_mirror > 0) {
1156                 io->ci_ndelay = 0;
1157                 io->ci_designated_mirror = fd->fd_designated_mirror;
1158                 io->ci_layout_version = fd->fd_layout_version;
1159                 io->ci_pio = 0; /* doesn't have a mechanism to pass mirror
1160                                  * io to ptasks */
1161         }
1162
1163         CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1164                file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1165 }
1166
1167 static bool file_is_noatime(const struct file *file)
1168 {
1169         const struct vfsmount *mnt = file->f_path.mnt;
1170         const struct inode *inode = file_inode((struct file *)file);
1171
1172         /* Adapted from file_accessed() and touch_atime().*/
1173         if (file->f_flags & O_NOATIME)
1174                 return true;
1175
1176         if (inode->i_flags & S_NOATIME)
1177                 return true;
1178
1179         if (IS_NOATIME(inode))
1180                 return true;
1181
1182         if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1183                 return true;
1184
1185         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1186                 return true;
1187
1188         if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1189                 return true;
1190
1191         return false;
1192 }
1193
1194 static int ll_file_io_ptask(struct cfs_ptask *ptask);
1195
1196 static void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot)
1197 {
1198         struct inode *inode = file_inode(file);
1199         struct ll_file_data *fd  = LUSTRE_FPRIVATE(file);
1200
1201         memset(&io->u.ci_rw.rw_iter, 0, sizeof(io->u.ci_rw.rw_iter));
1202         init_sync_kiocb(&io->u.ci_rw.rw_iocb, file);
1203         io->u.ci_rw.rw_file = file;
1204         io->u.ci_rw.rw_ptask = ll_file_io_ptask;
1205         io->u.ci_rw.rw_nonblock = !!(file->f_flags & O_NONBLOCK);
1206         io->ci_lock_no_expand = fd->ll_lock_no_expand;
1207
1208         if (iot == CIT_WRITE) {
1209                 io->u.ci_rw.rw_append = !!(file->f_flags & O_APPEND);
1210                 io->u.ci_rw.rw_sync   = !!(file->f_flags & O_SYNC ||
1211                                            file->f_flags & O_DIRECT ||
1212                                            IS_SYNC(inode));
1213         }
1214         io->ci_obj = ll_i2info(inode)->lli_clob;
1215         io->ci_lockreq = CILR_MAYBE;
1216         if (ll_file_nolock(file)) {
1217                 io->ci_lockreq = CILR_NEVER;
1218                 io->ci_no_srvlock = 1;
1219         } else if (file->f_flags & O_APPEND) {
1220                 io->ci_lockreq = CILR_MANDATORY;
1221         }
1222         io->ci_noatime = file_is_noatime(file);
1223         if (ll_i2sbi(inode)->ll_flags & LL_SBI_PIO)
1224                 io->ci_pio = !io->u.ci_rw.rw_append;
1225         else
1226                 io->ci_pio = 0;
1227
1228         /* FLR: only use non-delay I/O for read as there is only one
1229          * avaliable mirror for write. */
1230         io->ci_ndelay = !(iot == CIT_WRITE);
1231
1232         ll_io_set_mirror(io, file);
1233 }
1234
1235 static int ll_file_io_ptask(struct cfs_ptask *ptask)
1236 {
1237         struct cl_io_pt *pt = ptask->pt_cbdata;
1238         struct file *file = pt->cip_file;
1239         struct lu_env *env;
1240         struct cl_io *io;
1241         loff_t pos = pt->cip_pos;
1242         int rc;
1243         __u16 refcheck;
1244         ENTRY;
1245
1246         CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
1247                 file_dentry(file)->d_name.name,
1248                 pt->cip_iot == CIT_READ ? "read" : "write",
1249                 pos, pos + pt->cip_count);
1250
1251         env = cl_env_get(&refcheck);
1252         if (IS_ERR(env))
1253                 RETURN(PTR_ERR(env));
1254
1255         io = vvp_env_thread_io(env);
1256         ll_io_init(io, file, pt->cip_iot);
1257         io->u.ci_rw.rw_iter = pt->cip_iter;
1258         io->u.ci_rw.rw_iocb = pt->cip_iocb;
1259         io->ci_pio = 0; /* It's already in parallel task */
1260
1261         rc = cl_io_rw_init(env, io, pt->cip_iot, pos,
1262                            pt->cip_count - pt->cip_result);
1263         if (!rc) {
1264                 struct vvp_io *vio = vvp_env_io(env);
1265
1266                 vio->vui_io_subtype = IO_NORMAL;
1267                 vio->vui_fd = LUSTRE_FPRIVATE(file);
1268
1269                 ll_cl_add(file, env, io, LCC_RW);
1270                 rc = cl_io_loop(env, io);
1271                 ll_cl_remove(file, env);
1272         } else {
1273                 /* cl_io_rw_init() handled IO */
1274                 rc = io->ci_result;
1275         }
1276
1277         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_PTASK_IO_FAIL, 0)) {
1278                 if (io->ci_nob > 0)
1279                         io->ci_nob /= 2;
1280                 rc = -EIO;
1281         }
1282
1283         if (io->ci_nob > 0) {
1284                 pt->cip_result += io->ci_nob;
1285                 iov_iter_advance(&pt->cip_iter, io->ci_nob);
1286                 pos += io->ci_nob;
1287                 pt->cip_iocb.ki_pos = pos;
1288 #ifdef HAVE_KIOCB_KI_LEFT
1289                 pt->cip_iocb.ki_left = pt->cip_count - pt->cip_result;
1290 #elif defined(HAVE_KI_NBYTES)
1291                 pt->cip_iocb.ki_nbytes = pt->cip_count - pt->cip_result;
1292 #endif
1293         }
1294
1295         cl_io_fini(env, io);
1296         cl_env_put(env, &refcheck);
1297
1298         pt->cip_need_restart = io->ci_need_restart;
1299
1300         CDEBUG(D_VFSTRACE, "%s: %s ret: %zd, rc: %d\n",
1301                 file_dentry(file)->d_name.name,
1302                 pt->cip_iot == CIT_READ ? "read" : "write",
1303                 pt->cip_result, rc);
1304
1305         RETURN(pt->cip_result > 0 ? 0 : rc);
1306 }
1307
1308 static ssize_t
1309 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1310                    struct file *file, enum cl_io_type iot,
1311                    loff_t *ppos, size_t count)
1312 {
1313         struct range_lock       range;
1314         struct vvp_io           *vio = vvp_env_io(env);
1315         struct inode            *inode = file_inode(file);
1316         struct ll_inode_info    *lli = ll_i2info(inode);
1317         struct ll_file_data     *fd  = LUSTRE_FPRIVATE(file);
1318         struct cl_io            *io;
1319         loff_t                  pos = *ppos;
1320         ssize_t                 result = 0;
1321         int                     rc = 0;
1322         unsigned                retried = 0;
1323         bool                    restarted = false;
1324
1325         ENTRY;
1326
1327         CDEBUG(D_VFSTRACE, "%s: %s range: [%llu, %llu)\n",
1328                 file_dentry(file)->d_name.name,
1329                 iot == CIT_READ ? "read" : "write", pos, pos + count);
1330
1331 restart:
1332         io = vvp_env_thread_io(env);
1333         ll_io_init(io, file, iot);
1334         if (args->via_io_subtype == IO_NORMAL) {
1335                 io->u.ci_rw.rw_iter = *args->u.normal.via_iter;
1336                 io->u.ci_rw.rw_iocb = *args->u.normal.via_iocb;
1337         }
1338         if (args->via_io_subtype != IO_NORMAL || restarted)
1339                 io->ci_pio = 0;
1340         io->ci_ndelay_tried = retried;
1341
1342         if (cl_io_rw_init(env, io, iot, pos, count) == 0) {
1343                 bool range_locked = false;
1344
1345                 if (file->f_flags & O_APPEND)
1346                         range_lock_init(&range, 0, LUSTRE_EOF);
1347                 else
1348                         range_lock_init(&range, pos, pos + count - 1);
1349
1350                 vio->vui_fd  = LUSTRE_FPRIVATE(file);
1351                 vio->vui_io_subtype = args->via_io_subtype;
1352
1353                 switch (vio->vui_io_subtype) {
1354                 case IO_NORMAL:
1355                         /* Direct IO reads must also take range lock,
1356                          * or multiple reads will try to work on the same pages
1357                          * See LU-6227 for details. */
1358                         if (((iot == CIT_WRITE) ||
1359                             (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1360                             !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1361                                 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1362                                        RL_PARA(&range));
1363                                 rc = range_lock(&lli->lli_write_tree, &range);
1364                                 if (rc < 0)
1365                                         GOTO(out, rc);
1366
1367                                 range_locked = true;
1368                         }
1369                         break;
1370                 case IO_SPLICE:
1371                         vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1372                         vio->u.splice.vui_flags = args->u.splice.via_flags;
1373                         break;
1374                 default:
1375                         CERROR("unknown IO subtype %u\n", vio->vui_io_subtype);
1376                         LBUG();
1377                 }
1378
1379                 ll_cl_add(file, env, io, LCC_RW);
1380                 if (io->ci_pio && iot == CIT_WRITE && !IS_NOSEC(inode) &&
1381                     !lli->lli_inode_locked) {
1382                         inode_lock(inode);
1383                         lli->lli_inode_locked = 1;
1384                 }
1385                 rc = cl_io_loop(env, io);
1386                 if (lli->lli_inode_locked) {
1387                         lli->lli_inode_locked = 0;
1388                         inode_unlock(inode);
1389                 }
1390                 ll_cl_remove(file, env);
1391
1392                 if (range_locked) {
1393                         CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1394                                RL_PARA(&range));
1395                         range_unlock(&lli->lli_write_tree, &range);
1396                 }
1397         } else {
1398                 /* cl_io_rw_init() handled IO */
1399                 rc = io->ci_result;
1400         }
1401
1402         if (io->ci_nob > 0) {
1403                 result += io->ci_nob;
1404                 count  -= io->ci_nob;
1405
1406                 if (args->via_io_subtype == IO_NORMAL) {
1407                         iov_iter_advance(args->u.normal.via_iter, io->ci_nob);
1408                         pos += io->ci_nob;
1409                         args->u.normal.via_iocb->ki_pos = pos;
1410 #ifdef HAVE_KIOCB_KI_LEFT
1411                         args->u.normal.via_iocb->ki_left = count;
1412 #elif defined(HAVE_KI_NBYTES)
1413                         args->u.normal.via_iocb->ki_nbytes = count;
1414 #endif
1415                 } else {
1416                         /* for splice */
1417                         pos = io->u.ci_rw.rw_range.cir_pos;
1418                 }
1419         }
1420 out:
1421         cl_io_fini(env, io);
1422
1423         CDEBUG(D_VFSTRACE,
1424                "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1425                file->f_path.dentry->d_name.name,
1426                iot, rc, result, io->ci_need_restart);
1427
1428         if ((rc == 0 || rc == -ENODATA) && count > 0 && io->ci_need_restart) {
1429                 CDEBUG(D_VFSTRACE,
1430                         "%s: restart %s range: [%llu, %llu) ret: %zd, rc: %d\n",
1431                         file_dentry(file)->d_name.name,
1432                         iot == CIT_READ ? "read" : "write",
1433                         pos, pos + count, result, rc);
1434                 /* preserve the tried count for FLR */
1435                 retried = io->ci_ndelay_tried;
1436                 restarted = true;
1437                 goto restart;
1438         }
1439
1440         if (iot == CIT_READ) {
1441                 if (result > 0)
1442                         ll_stats_ops_tally(ll_i2sbi(inode),
1443                                            LPROC_LL_READ_BYTES, result);
1444         } else if (iot == CIT_WRITE) {
1445                 if (result > 0) {
1446                         ll_stats_ops_tally(ll_i2sbi(inode),
1447                                            LPROC_LL_WRITE_BYTES, result);
1448                         fd->fd_write_failed = false;
1449                 } else if (result == 0 && rc == 0) {
1450                         rc = io->ci_result;
1451                         if (rc < 0)
1452                                 fd->fd_write_failed = true;
1453                         else
1454                                 fd->fd_write_failed = false;
1455                 } else if (rc != -ERESTARTSYS) {
1456                         fd->fd_write_failed = true;
1457                 }
1458         }
1459
1460         CDEBUG(D_VFSTRACE, "%s: %s *ppos: %llu, pos: %llu, ret: %zd, rc: %d\n",
1461                 file_dentry(file)->d_name.name,
1462                 iot == CIT_READ ? "read" : "write", *ppos, pos, result, rc);
1463
1464         *ppos = pos;
1465
1466         RETURN(result > 0 ? result : rc);
1467 }
1468
1469 /**
1470  * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1471  * especially for small I/O.
1472  *
1473  * To serve a read request, CLIO has to create and initialize a cl_io and
1474  * then request DLM lock. This has turned out to have siginificant overhead
1475  * and affects the performance of small I/O dramatically.
1476  *
1477  * It's not necessary to create a cl_io for each I/O. Under the help of read
1478  * ahead, most of the pages being read are already in memory cache and we can
1479  * read those pages directly because if the pages exist, the corresponding DLM
1480  * lock must exist so that page content must be valid.
1481  *
1482  * In fast read implementation, the llite speculatively finds and reads pages
1483  * in memory cache. There are three scenarios for fast read:
1484  *   - If the page exists and is uptodate, kernel VM will provide the data and
1485  *     CLIO won't be intervened;
1486  *   - If the page was brought into memory by read ahead, it will be exported
1487  *     and read ahead parameters will be updated;
1488  *   - Otherwise the page is not in memory, we can't do fast read. Therefore,
1489  *     it will go back and invoke normal read, i.e., a cl_io will be created
1490  *     and DLM lock will be requested.
1491  *
1492  * POSIX compliance: posix standard states that read is intended to be atomic.
1493  * Lustre read implementation is in line with Linux kernel read implementation
1494  * and neither of them complies with POSIX standard in this matter. Fast read
1495  * doesn't make the situation worse on single node but it may interleave write
1496  * results from multiple nodes due to short read handling in ll_file_aio_read().
1497  *
1498  * \param env - lu_env
1499  * \param iocb - kiocb from kernel
1500  * \param iter - user space buffers where the data will be copied
1501  *
1502  * \retval - number of bytes have been read, or error code if error occurred.
1503  */
1504 static ssize_t
1505 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1506 {
1507         ssize_t result;
1508
1509         if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1510                 return 0;
1511
1512         /* NB: we can't do direct IO for fast read because it will need a lock
1513          * to make IO engine happy. */
1514         if (iocb->ki_filp->f_flags & O_DIRECT)
1515                 return 0;
1516
1517         result = generic_file_read_iter(iocb, iter);
1518
1519         /* If the first page is not in cache, generic_file_aio_read() will be
1520          * returned with -ENODATA.
1521          * See corresponding code in ll_readpage(). */
1522         if (result == -ENODATA)
1523                 result = 0;
1524
1525         if (result > 0)
1526                 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1527                                 LPROC_LL_READ_BYTES, result);
1528
1529         return result;
1530 }
1531
1532 /*
1533  * Read from a file (through the page cache).
1534  */
1535 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1536 {
1537         struct lu_env *env;
1538         struct vvp_io_args *args;
1539         ssize_t result;
1540         ssize_t rc2;
1541         __u16 refcheck;
1542
1543         result = ll_do_fast_read(iocb, to);
1544         if (result < 0 || iov_iter_count(to) == 0)
1545                 GOTO(out, result);
1546
1547         env = cl_env_get(&refcheck);
1548         if (IS_ERR(env))
1549                 return PTR_ERR(env);
1550
1551         args = ll_env_args(env, IO_NORMAL);
1552         args->u.normal.via_iter = to;
1553         args->u.normal.via_iocb = iocb;
1554
1555         rc2 = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1556                                  &iocb->ki_pos, iov_iter_count(to));
1557         if (rc2 > 0)
1558                 result += rc2;
1559         else if (result == 0)
1560                 result = rc2;
1561
1562         cl_env_put(env, &refcheck);
1563 out:
1564         return result;
1565 }
1566
1567 /**
1568  * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1569  * If a page is already in the page cache and dirty (and some other things -
1570  * See ll_tiny_write_begin for the instantiation of these rules), then we can
1571  * write to it without doing a full I/O, because Lustre already knows about it
1572  * and will write it out.  This saves a lot of processing time.
1573  *
1574  * All writes here are within one page, so exclusion is handled by the page
1575  * lock on the vm page.  We do not do tiny writes for writes which touch
1576  * multiple pages because it's very unlikely multiple sequential pages are
1577  * are already dirty.
1578  *
1579  * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1580  * and are unlikely to be to already dirty pages.
1581  *
1582  * Attribute updates are important here, we do them in ll_tiny_write_end.
1583  */
1584 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1585 {
1586         ssize_t count = iov_iter_count(iter);
1587         struct file *file = iocb->ki_filp;
1588         struct inode *inode = file_inode(file);
1589         ssize_t result = 0;
1590
1591         ENTRY;
1592
1593         /* Restrict writes to single page and < PAGE_SIZE.  See comment at top
1594          * of function for why.
1595          */
1596         if (count >= PAGE_SIZE ||
1597             (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1598                 RETURN(0);
1599
1600         result = __generic_file_write_iter(iocb, iter);
1601
1602         /* If the page is not already dirty, ll_tiny_write_begin returns
1603          * -ENODATA.  We continue on to normal write.
1604          */
1605         if (result == -ENODATA)
1606                 result = 0;
1607
1608         if (result > 0) {
1609                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1610                                    result);
1611                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1612         }
1613
1614         CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1615
1616         RETURN(result);
1617 }
1618
1619 /*
1620  * Write to a file (through the page cache).
1621  */
1622 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1623 {
1624         struct vvp_io_args *args;
1625         struct lu_env *env;
1626         ssize_t rc_tiny = 0, rc_normal;
1627         __u16 refcheck;
1628
1629         ENTRY;
1630
1631         /* NB: we can't do direct IO for tiny writes because they use the page
1632          * cache, we can't do sync writes because tiny writes can't flush
1633          * pages, and we can't do append writes because we can't guarantee the
1634          * required DLM locks are held to protect file size.
1635          */
1636         if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(iocb->ki_filp))) &&
1637             !(iocb->ki_filp->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
1638                 rc_tiny = ll_do_tiny_write(iocb, from);
1639
1640         /* In case of error, go on and try normal write - Only stop if tiny
1641          * write completed I/O.
1642          */
1643         if (iov_iter_count(from) == 0)
1644                 GOTO(out, rc_normal = rc_tiny);
1645
1646         env = cl_env_get(&refcheck);
1647         if (IS_ERR(env))
1648                 return PTR_ERR(env);
1649
1650         args = ll_env_args(env, IO_NORMAL);
1651         args->u.normal.via_iter = from;
1652         args->u.normal.via_iocb = iocb;
1653
1654         rc_normal = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1655                                     &iocb->ki_pos, iov_iter_count(from));
1656
1657         /* On success, combine bytes written. */
1658         if (rc_tiny >= 0 && rc_normal > 0)
1659                 rc_normal += rc_tiny;
1660         /* On error, only return error from normal write if tiny write did not
1661          * write any bytes.  Otherwise return bytes written by tiny write.
1662          */
1663         else if (rc_tiny > 0)
1664                 rc_normal = rc_tiny;
1665
1666         cl_env_put(env, &refcheck);
1667 out:
1668         RETURN(rc_normal);
1669 }
1670
1671 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1672 /*
1673  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1674  */
1675 static int ll_file_get_iov_count(const struct iovec *iov,
1676                                  unsigned long *nr_segs, size_t *count)
1677 {
1678         size_t cnt = 0;
1679         unsigned long seg;
1680
1681         for (seg = 0; seg < *nr_segs; seg++) {
1682                 const struct iovec *iv = &iov[seg];
1683
1684                 /*
1685                  * If any segment has a negative length, or the cumulative
1686                  * length ever wraps negative then return -EINVAL.
1687                  */
1688                 cnt += iv->iov_len;
1689                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1690                         return -EINVAL;
1691                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1692                         continue;
1693                 if (seg == 0)
1694                         return -EFAULT;
1695                 *nr_segs = seg;
1696                 cnt -= iv->iov_len;     /* This segment is no good */
1697                 break;
1698         }
1699         *count = cnt;
1700         return 0;
1701 }
1702
1703 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1704                                 unsigned long nr_segs, loff_t pos)
1705 {
1706         struct iov_iter to;
1707         size_t iov_count;
1708         ssize_t result;
1709         ENTRY;
1710
1711         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1712         if (result)
1713                 RETURN(result);
1714
1715 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1716         iov_iter_init(&to, READ, iov, nr_segs, iov_count);
1717 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1718         iov_iter_init(&to, iov, nr_segs, iov_count, 0);
1719 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1720
1721         result = ll_file_read_iter(iocb, &to);
1722
1723         RETURN(result);
1724 }
1725
1726 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
1727                             loff_t *ppos)
1728 {
1729         struct iovec   iov = { .iov_base = buf, .iov_len = count };
1730         struct kiocb   kiocb;
1731         ssize_t        result;
1732         ENTRY;
1733
1734         init_sync_kiocb(&kiocb, file);
1735         kiocb.ki_pos = *ppos;
1736 #ifdef HAVE_KIOCB_KI_LEFT
1737         kiocb.ki_left = count;
1738 #elif defined(HAVE_KI_NBYTES)
1739         kiocb.i_nbytes = count;
1740 #endif
1741
1742         result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
1743         *ppos = kiocb.ki_pos;
1744
1745         RETURN(result);
1746 }
1747
1748 /*
1749  * Write to a file (through the page cache).
1750  * AIO stuff
1751  */
1752 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1753                                  unsigned long nr_segs, loff_t pos)
1754 {
1755         struct iov_iter from;
1756         size_t iov_count;
1757         ssize_t result;
1758         ENTRY;
1759
1760         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1761         if (result)
1762                 RETURN(result);
1763
1764 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1765         iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
1766 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1767         iov_iter_init(&from, iov, nr_segs, iov_count, 0);
1768 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1769
1770         result = ll_file_write_iter(iocb, &from);
1771
1772         RETURN(result);
1773 }
1774
1775 static ssize_t ll_file_write(struct file *file, const char __user *buf,
1776                              size_t count, loff_t *ppos)
1777 {
1778         struct iovec   iov = { .iov_base = (void __user *)buf,
1779                                .iov_len = count };
1780         struct kiocb   kiocb;
1781         ssize_t        result;
1782
1783         ENTRY;
1784
1785         init_sync_kiocb(&kiocb, file);
1786         kiocb.ki_pos = *ppos;
1787 #ifdef HAVE_KIOCB_KI_LEFT
1788         kiocb.ki_left = count;
1789 #elif defined(HAVE_KI_NBYTES)
1790         kiocb.ki_nbytes = count;
1791 #endif
1792
1793         result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
1794         *ppos = kiocb.ki_pos;
1795
1796         RETURN(result);
1797 }
1798 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
1799
1800 /*
1801  * Send file content (through pagecache) somewhere with helper
1802  */
1803 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1804                                    struct pipe_inode_info *pipe, size_t count,
1805                                    unsigned int flags)
1806 {
1807         struct lu_env      *env;
1808         struct vvp_io_args *args;
1809         ssize_t             result;
1810         __u16               refcheck;
1811         ENTRY;
1812
1813         env = cl_env_get(&refcheck);
1814         if (IS_ERR(env))
1815                 RETURN(PTR_ERR(env));
1816
1817         args = ll_env_args(env, IO_SPLICE);
1818         args->u.splice.via_pipe = pipe;
1819         args->u.splice.via_flags = flags;
1820
1821         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1822         cl_env_put(env, &refcheck);
1823         RETURN(result);
1824 }
1825
1826 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
1827                              __u64 flags, struct lov_user_md *lum, int lum_size)
1828 {
1829         struct lookup_intent oit = {
1830                 .it_op = IT_OPEN,
1831                 .it_flags = flags | MDS_OPEN_BY_FID,
1832         };
1833         int rc;
1834         ENTRY;
1835
1836         ll_inode_size_lock(inode);
1837         rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
1838         if (rc < 0)
1839                 GOTO(out_unlock, rc);
1840
1841         ll_release_openhandle(dentry, &oit);
1842
1843 out_unlock:
1844         ll_inode_size_unlock(inode);
1845         ll_intent_release(&oit);
1846
1847         RETURN(rc);
1848 }
1849
1850 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1851                              struct lov_mds_md **lmmp, int *lmm_size,
1852                              struct ptlrpc_request **request)
1853 {
1854         struct ll_sb_info *sbi = ll_i2sbi(inode);
1855         struct mdt_body  *body;
1856         struct lov_mds_md *lmm = NULL;
1857         struct ptlrpc_request *req = NULL;
1858         struct md_op_data *op_data;
1859         int rc, lmmsize;
1860
1861         rc = ll_get_default_mdsize(sbi, &lmmsize);
1862         if (rc)
1863                 RETURN(rc);
1864
1865         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1866                                      strlen(filename), lmmsize,
1867                                      LUSTRE_OPC_ANY, NULL);
1868         if (IS_ERR(op_data))
1869                 RETURN(PTR_ERR(op_data));
1870
1871         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1872         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1873         ll_finish_md_op_data(op_data);
1874         if (rc < 0) {
1875                 CDEBUG(D_INFO, "md_getattr_name failed "
1876                        "on %s: rc %d\n", filename, rc);
1877                 GOTO(out, rc);
1878         }
1879
1880         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1881         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1882
1883         lmmsize = body->mbo_eadatasize;
1884
1885         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1886                         lmmsize == 0) {
1887                 GOTO(out, rc = -ENODATA);
1888         }
1889
1890         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1891         LASSERT(lmm != NULL);
1892
1893         if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
1894             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
1895             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1))
1896                 GOTO(out, rc = -EPROTO);
1897
1898         /*
1899          * This is coming from the MDS, so is probably in
1900          * little endian.  We convert it to host endian before
1901          * passing it to userspace.
1902          */
1903         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1904                 int stripe_count;
1905
1906                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
1907                     lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1908                         stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
1909                         if (le32_to_cpu(lmm->lmm_pattern) &
1910                             LOV_PATTERN_F_RELEASED)
1911                                 stripe_count = 0;
1912                 }
1913
1914                 /* if function called for directory - we should
1915                  * avoid swab not existent lsm objects */
1916                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1917                         lustre_swab_lov_user_md_v1(
1918                                         (struct lov_user_md_v1 *)lmm);
1919                         if (S_ISREG(body->mbo_mode))
1920                                 lustre_swab_lov_user_md_objects(
1921                                     ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1922                                     stripe_count);
1923                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1924                         lustre_swab_lov_user_md_v3(
1925                                         (struct lov_user_md_v3 *)lmm);
1926                         if (S_ISREG(body->mbo_mode))
1927                                 lustre_swab_lov_user_md_objects(
1928                                     ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1929                                     stripe_count);
1930                 } else if (lmm->lmm_magic ==
1931                            cpu_to_le32(LOV_MAGIC_COMP_V1)) {
1932                         lustre_swab_lov_comp_md_v1(
1933                                         (struct lov_comp_md_v1 *)lmm);
1934                 }
1935         }
1936
1937 out:
1938         *lmmp = lmm;
1939         *lmm_size = lmmsize;
1940         *request = req;
1941         return rc;
1942 }
1943
1944 static int ll_lov_setea(struct inode *inode, struct file *file,
1945                         void __user *arg)
1946 {
1947         __u64                    flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1948         struct lov_user_md      *lump;
1949         int                      lum_size = sizeof(struct lov_user_md) +
1950                                             sizeof(struct lov_user_ost_data);
1951         int                      rc;
1952         ENTRY;
1953
1954         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1955                 RETURN(-EPERM);
1956
1957         OBD_ALLOC_LARGE(lump, lum_size);
1958         if (lump == NULL)
1959                 RETURN(-ENOMEM);
1960
1961         if (copy_from_user(lump, arg, lum_size))
1962                 GOTO(out_lump, rc = -EFAULT);
1963
1964         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
1965                                       lum_size);
1966         cl_lov_delay_create_clear(&file->f_flags);
1967
1968 out_lump:
1969         OBD_FREE_LARGE(lump, lum_size);
1970         RETURN(rc);
1971 }
1972
1973 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
1974 {
1975         struct lu_env   *env;
1976         __u16           refcheck;
1977         int             rc;
1978         ENTRY;
1979
1980         env = cl_env_get(&refcheck);
1981         if (IS_ERR(env))
1982                 RETURN(PTR_ERR(env));
1983
1984         rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
1985         cl_env_put(env, &refcheck);
1986         RETURN(rc);
1987 }
1988
1989 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1990                             void __user *arg)
1991 {
1992         struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
1993         struct lov_user_md        *klum;
1994         int                        lum_size, rc;
1995         __u64                      flags = FMODE_WRITE;
1996         ENTRY;
1997
1998         rc = ll_copy_user_md(lum, &klum);
1999         if (rc < 0)
2000                 RETURN(rc);
2001
2002         lum_size = rc;
2003         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2004                                       lum_size);
2005         if (!rc) {
2006                 __u32 gen;
2007
2008                 rc = put_user(0, &lum->lmm_stripe_count);
2009                 if (rc)
2010                         GOTO(out, rc);
2011
2012                 rc = ll_layout_refresh(inode, &gen);
2013                 if (rc)
2014                         GOTO(out, rc);
2015
2016                 rc = ll_file_getstripe(inode, arg, lum_size);
2017         }
2018         cl_lov_delay_create_clear(&file->f_flags);
2019
2020 out:
2021         OBD_FREE(klum, lum_size);
2022         RETURN(rc);
2023 }
2024
2025 static int
2026 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2027 {
2028         struct ll_inode_info *lli = ll_i2info(inode);
2029         struct cl_object *obj = lli->lli_clob;
2030         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2031         struct ll_grouplock grouplock;
2032         int rc;
2033         ENTRY;
2034
2035         if (arg == 0) {
2036                 CWARN("group id for group lock must not be 0\n");
2037                 RETURN(-EINVAL);
2038         }
2039
2040         if (ll_file_nolock(file))
2041                 RETURN(-EOPNOTSUPP);
2042
2043         spin_lock(&lli->lli_lock);
2044         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2045                 CWARN("group lock already existed with gid %lu\n",
2046                       fd->fd_grouplock.lg_gid);
2047                 spin_unlock(&lli->lli_lock);
2048                 RETURN(-EINVAL);
2049         }
2050         LASSERT(fd->fd_grouplock.lg_lock == NULL);
2051         spin_unlock(&lli->lli_lock);
2052
2053         /**
2054          * XXX: group lock needs to protect all OST objects while PFL
2055          * can add new OST objects during the IO, so we'd instantiate
2056          * all OST objects before getting its group lock.
2057          */
2058         if (obj) {
2059                 struct lu_env *env;
2060                 __u16 refcheck;
2061                 struct cl_layout cl = {
2062                         .cl_is_composite = false,
2063                 };
2064                 struct lu_extent ext = {
2065                         .e_start = 0,
2066                         .e_end = OBD_OBJECT_EOF,
2067                 };
2068
2069                 env = cl_env_get(&refcheck);
2070                 if (IS_ERR(env))
2071                         RETURN(PTR_ERR(env));
2072
2073                 rc = cl_object_layout_get(env, obj, &cl);
2074                 if (!rc && cl.cl_is_composite)
2075                         rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2076                                                     &ext);
2077
2078                 cl_env_put(env, &refcheck);
2079                 if (rc)
2080                         RETURN(rc);
2081         }
2082
2083         rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2084                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
2085         if (rc)
2086                 RETURN(rc);
2087
2088         spin_lock(&lli->lli_lock);
2089         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2090                 spin_unlock(&lli->lli_lock);
2091                 CERROR("another thread just won the race\n");
2092                 cl_put_grouplock(&grouplock);
2093                 RETURN(-EINVAL);
2094         }
2095
2096         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2097         fd->fd_grouplock = grouplock;
2098         spin_unlock(&lli->lli_lock);
2099
2100         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2101         RETURN(0);
2102 }
2103
2104 static int ll_put_grouplock(struct inode *inode, struct file *file,
2105                             unsigned long arg)
2106 {
2107         struct ll_inode_info   *lli = ll_i2info(inode);
2108         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
2109         struct ll_grouplock     grouplock;
2110         ENTRY;
2111
2112         spin_lock(&lli->lli_lock);
2113         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2114                 spin_unlock(&lli->lli_lock);
2115                 CWARN("no group lock held\n");
2116                 RETURN(-EINVAL);
2117         }
2118
2119         LASSERT(fd->fd_grouplock.lg_lock != NULL);
2120
2121         if (fd->fd_grouplock.lg_gid != arg) {
2122                 CWARN("group lock %lu doesn't match current id %lu\n",
2123                       arg, fd->fd_grouplock.lg_gid);
2124                 spin_unlock(&lli->lli_lock);
2125                 RETURN(-EINVAL);
2126         }
2127
2128         grouplock = fd->fd_grouplock;
2129         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2130         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2131         spin_unlock(&lli->lli_lock);
2132
2133         cl_put_grouplock(&grouplock);
2134         CDEBUG(D_INFO, "group lock %lu released\n", arg);
2135         RETURN(0);
2136 }
2137
2138 /**
2139  * Close inode open handle
2140  *
2141  * \param dentry [in]     dentry which contains the inode
2142  * \param it     [in,out] intent which contains open info and result
2143  *
2144  * \retval 0     success
2145  * \retval <0    failure
2146  */
2147 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2148 {
2149         struct inode *inode = dentry->d_inode;
2150         struct obd_client_handle *och;
2151         int rc;
2152         ENTRY;
2153
2154         LASSERT(inode);
2155
2156         /* Root ? Do nothing. */
2157         if (dentry->d_inode->i_sb->s_root == dentry)
2158                 RETURN(0);
2159
2160         /* No open handle to close? Move away */
2161         if (!it_disposition(it, DISP_OPEN_OPEN))
2162                 RETURN(0);
2163
2164         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2165
2166         OBD_ALLOC(och, sizeof(*och));
2167         if (!och)
2168                 GOTO(out, rc = -ENOMEM);
2169
2170         ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2171
2172         rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2173 out:
2174         /* this one is in place of ll_file_open */
2175         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2176                 ptlrpc_req_finished(it->it_request);
2177                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2178         }
2179         RETURN(rc);
2180 }
2181
2182 /**
2183  * Get size for inode for which FIEMAP mapping is requested.
2184  * Make the FIEMAP get_info call and returns the result.
2185  * \param fiemap        kernel buffer to hold extens
2186  * \param num_bytes     kernel buffer size
2187  */
2188 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2189                         size_t num_bytes)
2190 {
2191         struct lu_env                   *env;
2192         __u16                           refcheck;
2193         int                             rc = 0;
2194         struct ll_fiemap_info_key       fmkey = { .lfik_name = KEY_FIEMAP, };
2195         ENTRY;
2196
2197         /* Checks for fiemap flags */
2198         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2199                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2200                 return -EBADR;
2201         }
2202
2203         /* Check for FIEMAP_FLAG_SYNC */
2204         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2205                 rc = filemap_fdatawrite(inode->i_mapping);
2206                 if (rc)
2207                         return rc;
2208         }
2209
2210         env = cl_env_get(&refcheck);
2211         if (IS_ERR(env))
2212                 RETURN(PTR_ERR(env));
2213
2214         if (i_size_read(inode) == 0) {
2215                 rc = ll_glimpse_size(inode);
2216                 if (rc)
2217                         GOTO(out, rc);
2218         }
2219
2220         fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2221         obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2222         obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2223
2224         /* If filesize is 0, then there would be no objects for mapping */
2225         if (fmkey.lfik_oa.o_size == 0) {
2226                 fiemap->fm_mapped_extents = 0;
2227                 GOTO(out, rc = 0);
2228         }
2229
2230         fmkey.lfik_fiemap = *fiemap;
2231
2232         rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2233                               &fmkey, fiemap, &num_bytes);
2234 out:
2235         cl_env_put(env, &refcheck);
2236         RETURN(rc);
2237 }
2238
2239 int ll_fid2path(struct inode *inode, void __user *arg)
2240 {
2241         struct obd_export       *exp = ll_i2mdexp(inode);
2242         const struct getinfo_fid2path __user *gfin = arg;
2243         __u32                    pathlen;
2244         struct getinfo_fid2path *gfout;
2245         size_t                   outsize;
2246         int                      rc;
2247
2248         ENTRY;
2249
2250         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2251             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2252                 RETURN(-EPERM);
2253
2254         /* Only need to get the buflen */
2255         if (get_user(pathlen, &gfin->gf_pathlen))
2256                 RETURN(-EFAULT);
2257
2258         if (pathlen > PATH_MAX)
2259                 RETURN(-EINVAL);
2260
2261         outsize = sizeof(*gfout) + pathlen;
2262         OBD_ALLOC(gfout, outsize);
2263         if (gfout == NULL)
2264                 RETURN(-ENOMEM);
2265
2266         if (copy_from_user(gfout, arg, sizeof(*gfout)))
2267                 GOTO(gf_free, rc = -EFAULT);
2268         /* append root FID after gfout to let MDT know the root FID so that it
2269          * can lookup the correct path, this is mainly for fileset.
2270          * old server without fileset mount support will ignore this. */
2271         *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2272
2273         /* Call mdc_iocontrol */
2274         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2275         if (rc != 0)
2276                 GOTO(gf_free, rc);
2277
2278         if (copy_to_user(arg, gfout, outsize))
2279                 rc = -EFAULT;
2280
2281 gf_free:
2282         OBD_FREE(gfout, outsize);
2283         RETURN(rc);
2284 }
2285
2286 static int
2287 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2288 {
2289         struct cl_object *obj = ll_i2info(inode)->lli_clob;
2290         struct lu_env *env;
2291         struct cl_io *io;
2292         __u16  refcheck;
2293         int result;
2294
2295         ENTRY;
2296
2297         ioc->idv_version = 0;
2298         ioc->idv_layout_version = UINT_MAX;
2299
2300         /* If no file object initialized, we consider its version is 0. */
2301         if (obj == NULL)
2302                 RETURN(0);
2303
2304         env = cl_env_get(&refcheck);
2305         if (IS_ERR(env))
2306                 RETURN(PTR_ERR(env));
2307
2308         io = vvp_env_thread_io(env);
2309         io->ci_obj = obj;
2310         io->u.ci_data_version.dv_data_version = 0;
2311         io->u.ci_data_version.dv_layout_version = UINT_MAX;
2312         io->u.ci_data_version.dv_flags = ioc->idv_flags;
2313
2314 restart:
2315         if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2316                 result = cl_io_loop(env, io);
2317         else
2318                 result = io->ci_result;
2319
2320         ioc->idv_version = io->u.ci_data_version.dv_data_version;
2321         ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2322
2323         cl_io_fini(env, io);
2324
2325         if (unlikely(io->ci_need_restart))
2326                 goto restart;
2327
2328         cl_env_put(env, &refcheck);
2329
2330         RETURN(result);
2331 }
2332
2333 /*
2334  * Read the data_version for inode.
2335  *
2336  * This value is computed using stripe object version on OST.
2337  * Version is computed using server side locking.
2338  *
2339  * @param flags if do sync on the OST side;
2340  *              0: no sync
2341  *              LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2342  *              LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2343  */
2344 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2345 {
2346         struct ioc_data_version ioc = { .idv_flags = flags };
2347         int rc;
2348
2349         rc = ll_ioc_data_version(inode, &ioc);
2350         if (!rc)
2351                 *data_version = ioc.idv_version;
2352
2353         return rc;
2354 }
2355
2356 /*
2357  * Trigger a HSM release request for the provided inode.
2358  */
2359 int ll_hsm_release(struct inode *inode)
2360 {
2361         struct lu_env *env;
2362         struct obd_client_handle *och = NULL;
2363         __u64 data_version = 0;
2364         int rc;
2365         __u16 refcheck;
2366         ENTRY;
2367
2368         CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2369                ll_get_fsname(inode->i_sb, NULL, 0),
2370                PFID(&ll_i2info(inode)->lli_fid));
2371
2372         och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2373         if (IS_ERR(och))
2374                 GOTO(out, rc = PTR_ERR(och));
2375
2376         /* Grab latest data_version and [am]time values */
2377         rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2378         if (rc != 0)
2379                 GOTO(out, rc);
2380
2381         env = cl_env_get(&refcheck);
2382         if (IS_ERR(env))
2383                 GOTO(out, rc = PTR_ERR(env));
2384
2385         rc = ll_merge_attr(env, inode);
2386         cl_env_put(env, &refcheck);
2387
2388         /* If error happen, we have the wrong size for a file.
2389          * Don't release it.
2390          */
2391         if (rc != 0)
2392                 GOTO(out, rc);
2393
2394         /* Release the file.
2395          * NB: lease lock handle is released in mdc_hsm_release_pack() because
2396          * we still need it to pack l_remote_handle to MDT. */
2397         rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2398                                        &data_version);
2399         och = NULL;
2400
2401         EXIT;
2402 out:
2403         if (och != NULL && !IS_ERR(och)) /* close the file */
2404                 ll_lease_close(och, inode, NULL);
2405
2406         return rc;
2407 }
2408
2409 struct ll_swap_stack {
2410         __u64                    dv1;
2411         __u64                    dv2;
2412         struct inode            *inode1;
2413         struct inode            *inode2;
2414         bool                     check_dv1;
2415         bool                     check_dv2;
2416 };
2417
2418 static int ll_swap_layouts(struct file *file1, struct file *file2,
2419                            struct lustre_swap_layouts *lsl)
2420 {
2421         struct mdc_swap_layouts  msl;
2422         struct md_op_data       *op_data;
2423         __u32                    gid;
2424         __u64                    dv;
2425         struct ll_swap_stack    *llss = NULL;
2426         int                      rc;
2427
2428         OBD_ALLOC_PTR(llss);
2429         if (llss == NULL)
2430                 RETURN(-ENOMEM);
2431
2432         llss->inode1 = file_inode(file1);
2433         llss->inode2 = file_inode(file2);
2434
2435         rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2436         if (rc < 0)
2437                 GOTO(free, rc);
2438
2439         /* we use 2 bool because it is easier to swap than 2 bits */
2440         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2441                 llss->check_dv1 = true;
2442
2443         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2444                 llss->check_dv2 = true;
2445
2446         /* we cannot use lsl->sl_dvX directly because we may swap them */
2447         llss->dv1 = lsl->sl_dv1;
2448         llss->dv2 = lsl->sl_dv2;
2449
2450         rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2451         if (rc == 0) /* same file, done! */
2452                 GOTO(free, rc);
2453
2454         if (rc < 0) { /* sequentialize it */
2455                 swap(llss->inode1, llss->inode2);
2456                 swap(file1, file2);
2457                 swap(llss->dv1, llss->dv2);
2458                 swap(llss->check_dv1, llss->check_dv2);
2459         }
2460
2461         gid = lsl->sl_gid;
2462         if (gid != 0) { /* application asks to flush dirty cache */
2463                 rc = ll_get_grouplock(llss->inode1, file1, gid);
2464                 if (rc < 0)
2465                         GOTO(free, rc);
2466
2467                 rc = ll_get_grouplock(llss->inode2, file2, gid);
2468                 if (rc < 0) {
2469                         ll_put_grouplock(llss->inode1, file1, gid);
2470                         GOTO(free, rc);
2471                 }
2472         }
2473
2474         /* ultimate check, before swaping the layouts we check if
2475          * dataversion has changed (if requested) */
2476         if (llss->check_dv1) {
2477                 rc = ll_data_version(llss->inode1, &dv, 0);
2478                 if (rc)
2479                         GOTO(putgl, rc);
2480                 if (dv != llss->dv1)
2481                         GOTO(putgl, rc = -EAGAIN);
2482         }
2483
2484         if (llss->check_dv2) {
2485                 rc = ll_data_version(llss->inode2, &dv, 0);
2486                 if (rc)
2487                         GOTO(putgl, rc);
2488                 if (dv != llss->dv2)
2489                         GOTO(putgl, rc = -EAGAIN);
2490         }
2491
2492         /* struct md_op_data is used to send the swap args to the mdt
2493          * only flags is missing, so we use struct mdc_swap_layouts
2494          * through the md_op_data->op_data */
2495         /* flags from user space have to be converted before they are send to
2496          * server, no flag is sent today, they are only used on the client */
2497         msl.msl_flags = 0;
2498         rc = -ENOMEM;
2499         op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2500                                      0, LUSTRE_OPC_ANY, &msl);
2501         if (IS_ERR(op_data))
2502                 GOTO(free, rc = PTR_ERR(op_data));
2503
2504         rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2505                            sizeof(*op_data), op_data, NULL);
2506         ll_finish_md_op_data(op_data);
2507
2508         if (rc < 0)
2509                 GOTO(putgl, rc);
2510
2511 putgl:
2512         if (gid != 0) {
2513                 ll_put_grouplock(llss->inode2, file2, gid);
2514                 ll_put_grouplock(llss->inode1, file1, gid);
2515         }
2516
2517 free:
2518         if (llss != NULL)
2519                 OBD_FREE_PTR(llss);
2520
2521         RETURN(rc);
2522 }
2523
2524 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2525 {
2526         struct md_op_data       *op_data;
2527         int                      rc;
2528         ENTRY;
2529
2530         /* Detect out-of range masks */
2531         if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2532                 RETURN(-EINVAL);
2533
2534         /* Non-root users are forbidden to set or clear flags which are
2535          * NOT defined in HSM_USER_MASK. */
2536         if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2537             !cfs_capable(CFS_CAP_SYS_ADMIN))
2538                 RETURN(-EPERM);
2539
2540         /* Detect out-of range archive id */
2541         if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2542             (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE))
2543                 RETURN(-EINVAL);
2544
2545         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2546                                      LUSTRE_OPC_ANY, hss);
2547         if (IS_ERR(op_data))
2548                 RETURN(PTR_ERR(op_data));
2549
2550         rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, ll_i2mdexp(inode),
2551                            sizeof(*op_data), op_data, NULL);
2552
2553         ll_finish_md_op_data(op_data);
2554
2555         RETURN(rc);
2556 }
2557
2558 static int ll_hsm_import(struct inode *inode, struct file *file,
2559                          struct hsm_user_import *hui)
2560 {
2561         struct hsm_state_set    *hss = NULL;
2562         struct iattr            *attr = NULL;
2563         int                      rc;
2564         ENTRY;
2565
2566         if (!S_ISREG(inode->i_mode))
2567                 RETURN(-EINVAL);
2568
2569         /* set HSM flags */
2570         OBD_ALLOC_PTR(hss);
2571         if (hss == NULL)
2572                 GOTO(out, rc = -ENOMEM);
2573
2574         hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2575         hss->hss_archive_id = hui->hui_archive_id;
2576         hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2577         rc = ll_hsm_state_set(inode, hss);
2578         if (rc != 0)
2579                 GOTO(out, rc);
2580
2581         OBD_ALLOC_PTR(attr);
2582         if (attr == NULL)
2583                 GOTO(out, rc = -ENOMEM);
2584
2585         attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2586         attr->ia_mode |= S_IFREG;
2587         attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2588         attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2589         attr->ia_size = hui->hui_size;
2590         attr->ia_mtime.tv_sec = hui->hui_mtime;
2591         attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2592         attr->ia_atime.tv_sec = hui->hui_atime;
2593         attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2594
2595         attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2596                          ATTR_UID | ATTR_GID |
2597                          ATTR_MTIME | ATTR_MTIME_SET |
2598                          ATTR_ATIME | ATTR_ATIME_SET;
2599
2600         inode_lock(inode);
2601
2602         rc = ll_setattr_raw(file_dentry(file), attr, true);
2603         if (rc == -ENODATA)
2604                 rc = 0;
2605
2606         inode_unlock(inode);
2607
2608 out:
2609         if (hss != NULL)
2610                 OBD_FREE_PTR(hss);
2611
2612         if (attr != NULL)
2613                 OBD_FREE_PTR(attr);
2614
2615         RETURN(rc);
2616 }
2617
2618 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2619 {
2620         return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2621                ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2622 }
2623
2624 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2625 {
2626         struct inode *inode = file_inode(file);
2627         struct iattr ia = {
2628                 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2629                             ATTR_MTIME | ATTR_MTIME_SET |
2630                             ATTR_CTIME | ATTR_CTIME_SET,
2631                 .ia_atime = {
2632                         .tv_sec = lfu->lfu_atime_sec,
2633                         .tv_nsec = lfu->lfu_atime_nsec,
2634                 },
2635                 .ia_mtime = {
2636                         .tv_sec = lfu->lfu_mtime_sec,
2637                         .tv_nsec = lfu->lfu_mtime_nsec,
2638                 },
2639                 .ia_ctime = {
2640                         .tv_sec = lfu->lfu_ctime_sec,
2641                         .tv_nsec = lfu->lfu_ctime_nsec,
2642                 },
2643         };
2644         int rc;
2645         ENTRY;
2646
2647         if (!capable(CAP_SYS_ADMIN))
2648                 RETURN(-EPERM);
2649
2650         if (!S_ISREG(inode->i_mode))
2651                 RETURN(-EINVAL);
2652
2653         inode_lock(inode);
2654         rc = ll_setattr_raw(file_dentry(file), &ia, false);
2655         inode_unlock(inode);
2656
2657         RETURN(rc);
2658 }
2659
2660 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
2661 {
2662         switch (mode) {
2663         case MODE_READ_USER:
2664                 return CLM_READ;
2665         case MODE_WRITE_USER:
2666                 return CLM_WRITE;
2667         default:
2668                 return -EINVAL;
2669         }
2670 }
2671
2672 static const char *const user_lockname[] = LOCK_MODE_NAMES;
2673
2674 /* Used to allow the upper layers of the client to request an LDLM lock
2675  * without doing an actual read or write.
2676  *
2677  * Used for ladvise lockahead to manually request specific locks.
2678  *
2679  * \param[in] file      file this ladvise lock request is on
2680  * \param[in] ladvise   ladvise struct describing this lock request
2681  *
2682  * \retval 0            success, no detailed result available (sync requests
2683  *                      and requests sent to the server [not handled locally]
2684  *                      cannot return detailed results)
2685  * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
2686  *                                       see definitions for details.
2687  * \retval negative     negative errno on error
2688  */
2689 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
2690 {
2691         struct lu_env *env = NULL;
2692         struct cl_io *io  = NULL;
2693         struct cl_lock *lock = NULL;
2694         struct cl_lock_descr *descr = NULL;
2695         struct dentry *dentry = file->f_path.dentry;
2696         struct inode *inode = dentry->d_inode;
2697         enum cl_lock_mode cl_mode;
2698         off_t start = ladvise->lla_start;
2699         off_t end = ladvise->lla_end;
2700         int result;
2701         __u16 refcheck;
2702
2703         ENTRY;
2704
2705         CDEBUG(D_VFSTRACE, "Lock request: file=%.*s, inode=%p, mode=%s "
2706                "start=%llu, end=%llu\n", dentry->d_name.len,
2707                dentry->d_name.name, dentry->d_inode,
2708                user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
2709                (__u64) end);
2710
2711         cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
2712         if (cl_mode < 0)
2713                 GOTO(out, result = cl_mode);
2714
2715         /* Get IO environment */
2716         result = cl_io_get(inode, &env, &io, &refcheck);
2717         if (result <= 0)
2718                 GOTO(out, result);
2719
2720         result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
2721         if (result > 0) {
2722                 /*
2723                  * nothing to do for this io. This currently happens when
2724                  * stripe sub-object's are not yet created.
2725                  */
2726                 result = io->ci_result;
2727         } else if (result == 0) {
2728                 lock = vvp_env_lock(env);
2729                 descr = &lock->cll_descr;
2730
2731                 descr->cld_obj   = io->ci_obj;
2732                 /* Convert byte offsets to pages */
2733                 descr->cld_start = cl_index(io->ci_obj, start);
2734                 descr->cld_end   = cl_index(io->ci_obj, end);
2735                 descr->cld_mode  = cl_mode;
2736                 /* CEF_MUST is used because we do not want to convert a
2737                  * lockahead request to a lockless lock */
2738                 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
2739                                        CEF_NONBLOCK;
2740
2741                 if (ladvise->lla_peradvice_flags & LF_ASYNC)
2742                         descr->cld_enq_flags |= CEF_SPECULATIVE;
2743
2744                 result = cl_lock_request(env, io, lock);
2745
2746                 /* On success, we need to release the lock */
2747                 if (result >= 0)
2748                         cl_lock_release(env, lock);
2749         }
2750         cl_io_fini(env, io);
2751         cl_env_put(env, &refcheck);
2752
2753         /* -ECANCELED indicates a matching lock with a different extent
2754          * was already present, and -EEXIST indicates a matching lock
2755          * on exactly the same extent was already present.
2756          * We convert them to positive values for userspace to make
2757          * recognizing true errors easier.
2758          * Note we can only return these detailed results on async requests,
2759          * as sync requests look the same as i/o requests for locking. */
2760         if (result == -ECANCELED)
2761                 result = LLA_RESULT_DIFFERENT;
2762         else if (result == -EEXIST)
2763                 result = LLA_RESULT_SAME;
2764
2765 out:
2766         RETURN(result);
2767 }
2768 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
2769
2770 static int ll_ladvise_sanity(struct inode *inode,
2771                              struct llapi_lu_ladvise *ladvise)
2772 {
2773         enum lu_ladvise_type advice = ladvise->lla_advice;
2774         /* Note the peradvice flags is a 32 bit field, so per advice flags must
2775          * be in the first 32 bits of enum ladvise_flags */
2776         __u32 flags = ladvise->lla_peradvice_flags;
2777         /* 3 lines at 80 characters per line, should be plenty */
2778         int rc = 0;
2779
2780         if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
2781                 rc = -EINVAL;
2782                 CDEBUG(D_VFSTRACE, "%s: advice with value '%d' not recognized,"
2783                        "last supported advice is %s (value '%d'): rc = %d\n",
2784                        ll_get_fsname(inode->i_sb, NULL, 0), advice,
2785                        ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
2786                 GOTO(out, rc);
2787         }
2788
2789         /* Per-advice checks */
2790         switch (advice) {
2791         case LU_LADVISE_LOCKNOEXPAND:
2792                 if (flags & ~LF_LOCKNOEXPAND_MASK) {
2793                         rc = -EINVAL;
2794                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
2795                                "rc = %d\n",
2796                                ll_get_fsname(inode->i_sb, NULL, 0), flags,
2797                                ladvise_names[advice], rc);
2798                         GOTO(out, rc);
2799                 }
2800                 break;
2801         case LU_LADVISE_LOCKAHEAD:
2802                 /* Currently only READ and WRITE modes can be requested */
2803                 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
2804                     ladvise->lla_lockahead_mode == 0) {
2805                         rc = -EINVAL;
2806                         CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
2807                                "rc = %d\n",
2808                                ll_get_fsname(inode->i_sb, NULL, 0),
2809                                ladvise->lla_lockahead_mode,
2810                                ladvise_names[advice], rc);
2811                         GOTO(out, rc);
2812                 }
2813         case LU_LADVISE_WILLREAD:
2814         case LU_LADVISE_DONTNEED:
2815         default:
2816                 /* Note fall through above - These checks apply to all advices
2817                  * except LOCKNOEXPAND */
2818                 if (flags & ~LF_DEFAULT_MASK) {
2819                         rc = -EINVAL;
2820                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
2821                                "rc = %d\n",
2822                                ll_get_fsname(inode->i_sb, NULL, 0), flags,
2823                                ladvise_names[advice], rc);
2824                         GOTO(out, rc);
2825                 }
2826                 if (ladvise->lla_start >= ladvise->lla_end) {
2827                         rc = -EINVAL;
2828                         CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
2829                                "for %s: rc = %d\n",
2830                                ll_get_fsname(inode->i_sb, NULL, 0),
2831                                ladvise->lla_start, ladvise->lla_end,
2832                                ladvise_names[advice], rc);
2833                         GOTO(out, rc);
2834                 }
2835                 break;
2836         }
2837
2838 out:
2839         return rc;
2840 }
2841 #undef ERRSIZE
2842
2843 /*
2844  * Give file access advices
2845  *
2846  * The ladvise interface is similar to Linux fadvise() system call, except it
2847  * forwards the advices directly from Lustre client to server. The server side
2848  * codes will apply appropriate read-ahead and caching techniques for the
2849  * corresponding files.
2850  *
2851  * A typical workload for ladvise is e.g. a bunch of different clients are
2852  * doing small random reads of a file, so prefetching pages into OSS cache
2853  * with big linear reads before the random IO is a net benefit. Fetching
2854  * all that data into each client cache with fadvise() may not be, due to
2855  * much more data being sent to the client.
2856  */
2857 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
2858                       struct llapi_lu_ladvise *ladvise)
2859 {
2860         struct lu_env *env;
2861         struct cl_io *io;
2862         struct cl_ladvise_io *lio;
2863         int rc;
2864         __u16 refcheck;
2865         ENTRY;
2866
2867         env = cl_env_get(&refcheck);
2868         if (IS_ERR(env))
2869                 RETURN(PTR_ERR(env));
2870
2871         io = vvp_env_thread_io(env);
2872         io->ci_obj = ll_i2info(inode)->lli_clob;
2873
2874         /* initialize parameters for ladvise */
2875         lio = &io->u.ci_ladvise;
2876         lio->li_start = ladvise->lla_start;
2877         lio->li_end = ladvise->lla_end;
2878         lio->li_fid = ll_inode2fid(inode);
2879         lio->li_advice = ladvise->lla_advice;
2880         lio->li_flags = flags;
2881
2882         if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
2883                 rc = cl_io_loop(env, io);
2884         else
2885                 rc = io->ci_result;
2886
2887         cl_io_fini(env, io);
2888         cl_env_put(env, &refcheck);
2889         RETURN(rc);
2890 }
2891
2892 static int ll_lock_noexpand(struct file *file, int flags)
2893 {
2894         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2895
2896         fd->ll_lock_no_expand = !(flags & LF_UNSET);
2897
2898         return 0;
2899 }
2900
2901 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
2902                         unsigned long arg)
2903 {
2904         struct fsxattr fsxattr;
2905
2906         if (copy_from_user(&fsxattr,
2907                            (const struct fsxattr __user *)arg,
2908                            sizeof(fsxattr)))
2909                 RETURN(-EFAULT);
2910
2911         fsxattr.fsx_xflags = ll_inode_to_ext_flags(inode->i_flags);
2912         fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
2913         if (copy_to_user((struct fsxattr __user *)arg,
2914                          &fsxattr, sizeof(fsxattr)))
2915                 RETURN(-EFAULT);
2916
2917         RETURN(0);
2918 }
2919
2920 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
2921                         unsigned long arg)
2922 {
2923
2924         struct md_op_data *op_data;
2925         struct ptlrpc_request *req = NULL;
2926         int rc = 0;
2927         struct fsxattr fsxattr;
2928         struct cl_object *obj;
2929
2930         /* only root could change project ID */
2931         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2932                 RETURN(-EPERM);
2933
2934         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2935                                      LUSTRE_OPC_ANY, NULL);
2936         if (IS_ERR(op_data))
2937                 RETURN(PTR_ERR(op_data));
2938
2939         if (copy_from_user(&fsxattr,
2940                            (const struct fsxattr __user *)arg,
2941                            sizeof(fsxattr)))
2942                 GOTO(out_fsxattr1, rc = -EFAULT);
2943
2944         op_data->op_attr_flags = fsxattr.fsx_xflags;
2945         op_data->op_projid = fsxattr.fsx_projid;
2946         op_data->op_attr.ia_valid |= (MDS_ATTR_PROJID | ATTR_ATTR_FLAG);
2947         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
2948                         0, &req);
2949         ptlrpc_req_finished(req);
2950
2951         obj = ll_i2info(inode)->lli_clob;
2952         if (obj) {
2953                 struct iattr *attr;
2954
2955                 inode->i_flags = ll_ext_to_inode_flags(fsxattr.fsx_xflags);
2956                 OBD_ALLOC_PTR(attr);
2957                 if (attr == NULL)
2958                         GOTO(out_fsxattr1, rc = -ENOMEM);
2959                 attr->ia_valid = ATTR_ATTR_FLAG;
2960                 rc = cl_setattr_ost(obj, attr, fsxattr.fsx_xflags);
2961
2962                 OBD_FREE_PTR(attr);
2963         }
2964 out_fsxattr1:
2965         ll_finish_md_op_data(op_data);
2966         RETURN(rc);
2967 }
2968
2969 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
2970                                  unsigned long arg)
2971 {
2972         struct inode            *inode = file_inode(file);
2973         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
2974         struct ll_inode_info    *lli = ll_i2info(inode);
2975         struct obd_client_handle *och = NULL;
2976         struct split_param sp;
2977         bool lease_broken;
2978         fmode_t fmode = 0;
2979         enum mds_op_bias bias = 0;
2980         struct file *layout_file = NULL;
2981         void *data = NULL;
2982         size_t data_size = 0;
2983         long rc;
2984         ENTRY;
2985
2986         mutex_lock(&lli->lli_och_mutex);
2987         if (fd->fd_lease_och != NULL) {
2988                 och = fd->fd_lease_och;
2989                 fd->fd_lease_och = NULL;
2990         }
2991         mutex_unlock(&lli->lli_och_mutex);
2992
2993         if (och == NULL)
2994                 GOTO(out, rc = -ENOLCK);
2995
2996         fmode = och->och_flags;
2997
2998         switch (ioc->lil_flags) {
2999         case LL_LEASE_RESYNC_DONE:
3000                 if (ioc->lil_count > IOC_IDS_MAX)
3001                         GOTO(out, rc = -EINVAL);
3002
3003                 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3004                 OBD_ALLOC(data, data_size);
3005                 if (!data)
3006                         GOTO(out, rc = -ENOMEM);
3007
3008                 if (copy_from_user(data, (void __user *)arg, data_size))
3009                         GOTO(out, rc = -EFAULT);
3010
3011                 bias = MDS_CLOSE_RESYNC_DONE;
3012                 break;
3013         case LL_LEASE_LAYOUT_MERGE: {
3014                 int fd;
3015
3016                 if (ioc->lil_count != 1)
3017                         GOTO(out, rc = -EINVAL);
3018
3019                 arg += sizeof(*ioc);
3020                 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3021                         GOTO(out, rc = -EFAULT);
3022
3023                 layout_file = fget(fd);
3024                 if (!layout_file)
3025                         GOTO(out, rc = -EBADF);
3026
3027                 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3028                                 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3029                         GOTO(out, rc = -EPERM);
3030
3031                 data = file_inode(layout_file);
3032                 bias = MDS_CLOSE_LAYOUT_MERGE;
3033                 break;
3034         }
3035         case LL_LEASE_LAYOUT_SPLIT: {
3036                 int fdv;
3037                 int mirror_id;
3038
3039                 if (ioc->lil_count != 2)
3040                         GOTO(out, rc = -EINVAL);
3041
3042                 arg += sizeof(*ioc);
3043                 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3044                         GOTO(out, rc = -EFAULT);
3045
3046                 arg += sizeof(__u32);
3047                 if (copy_from_user(&mirror_id, (void __user *)arg,
3048                                    sizeof(__u32)))
3049                         GOTO(out, rc = -EFAULT);
3050
3051                 layout_file = fget(fdv);
3052                 if (!layout_file)
3053                         GOTO(out, rc = -EBADF);
3054
3055                 sp.sp_inode = file_inode(layout_file);
3056                 sp.sp_mirror_id = (__u16)mirror_id;
3057                 data = &sp;
3058                 bias = MDS_CLOSE_LAYOUT_SPLIT;
3059                 break;
3060         }
3061         default:
3062                 /* without close intent */
3063                 break;
3064         }
3065
3066         rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3067         if (rc < 0)
3068                 GOTO(out, rc);
3069
3070         rc = ll_lease_och_release(inode, file);
3071         if (rc < 0)
3072                 GOTO(out, rc);
3073
3074         if (lease_broken)
3075                 fmode = 0;
3076         EXIT;
3077
3078 out:
3079         switch (ioc->lil_flags) {
3080         case LL_LEASE_RESYNC_DONE:
3081                 if (data)
3082                         OBD_FREE(data, data_size);
3083                 break;
3084         case LL_LEASE_LAYOUT_MERGE:
3085         case LL_LEASE_LAYOUT_SPLIT:
3086                 if (layout_file)
3087                         fput(layout_file);
3088                 break;
3089         }
3090
3091         if (!rc)
3092                 rc = ll_lease_type_from_fmode(fmode);
3093         RETURN(rc);
3094 }
3095
3096 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3097                               unsigned long arg)
3098 {
3099         struct inode *inode = file_inode(file);
3100         struct ll_inode_info *lli = ll_i2info(inode);
3101         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3102         struct obd_client_handle *och = NULL;
3103         __u64 open_flags = 0;
3104         bool lease_broken;
3105         fmode_t fmode;
3106         long rc;
3107         ENTRY;
3108
3109         switch (ioc->lil_mode) {
3110         case LL_LEASE_WRLCK:
3111                 if (!(file->f_mode & FMODE_WRITE))
3112                         RETURN(-EPERM);
3113                 fmode = FMODE_WRITE;
3114                 break;
3115         case LL_LEASE_RDLCK:
3116                 if (!(file->f_mode & FMODE_READ))
3117                         RETURN(-EPERM);
3118                 fmode = FMODE_READ;
3119                 break;
3120         case LL_LEASE_UNLCK:
3121                 RETURN(ll_file_unlock_lease(file, ioc, arg));
3122         default:
3123                 RETURN(-EINVAL);
3124         }
3125
3126         CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3127
3128         /* apply for lease */
3129         if (ioc->lil_flags & LL_LEASE_RESYNC)
3130                 open_flags = MDS_OPEN_RESYNC;
3131         och = ll_lease_open(inode, file, fmode, open_flags);
3132         if (IS_ERR(och))
3133                 RETURN(PTR_ERR(och));
3134
3135         if (ioc->lil_flags & LL_LEASE_RESYNC) {
3136                 rc = ll_lease_file_resync(och, inode);
3137                 if (rc) {
3138                         ll_lease_close(och, inode, NULL);
3139                         RETURN(rc);
3140                 }
3141                 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3142                 if (rc) {
3143                         ll_lease_close(och, inode, NULL);
3144                         RETURN(rc);
3145                 }
3146         }
3147
3148         rc = 0;
3149         mutex_lock(&lli->lli_och_mutex);
3150         if (fd->fd_lease_och == NULL) {
3151                 fd->fd_lease_och = och;
3152                 och = NULL;
3153         }
3154         mutex_unlock(&lli->lli_och_mutex);
3155         if (och != NULL) {
3156                 /* impossible now that only excl is supported for now */
3157                 ll_lease_close(och, inode, &lease_broken);
3158                 rc = -EBUSY;
3159         }
3160         RETURN(rc);
3161 }
3162
3163 static long
3164 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3165 {
3166         struct inode            *inode = file_inode(file);
3167         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
3168         int                      flags, rc;
3169         ENTRY;
3170
3171         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3172                PFID(ll_inode2fid(inode)), inode, cmd);
3173         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3174
3175         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3176         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3177                 RETURN(-ENOTTY);
3178
3179         switch(cmd) {
3180         case LL_IOC_GETFLAGS:
3181                 /* Get the current value of the file flags */
3182                 return put_user(fd->fd_flags, (int __user *)arg);
3183         case LL_IOC_SETFLAGS:
3184         case LL_IOC_CLRFLAGS:
3185                 /* Set or clear specific file flags */
3186                 /* XXX This probably needs checks to ensure the flags are
3187                  *     not abused, and to handle any flag side effects.
3188                  */
3189                 if (get_user(flags, (int __user *) arg))
3190                         RETURN(-EFAULT);
3191
3192                 if (cmd == LL_IOC_SETFLAGS) {
3193                         if ((flags & LL_FILE_IGNORE_LOCK) &&
3194                             !(file->f_flags & O_DIRECT)) {
3195                                 CERROR("%s: unable to disable locking on "
3196                                        "non-O_DIRECT file\n", current->comm);
3197                                 RETURN(-EINVAL);
3198                         }
3199
3200                         fd->fd_flags |= flags;
3201                 } else {
3202                         fd->fd_flags &= ~flags;
3203                 }
3204                 RETURN(0);
3205         case LL_IOC_LOV_SETSTRIPE:
3206         case LL_IOC_LOV_SETSTRIPE_NEW:
3207                 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3208         case LL_IOC_LOV_SETEA:
3209                 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3210         case LL_IOC_LOV_SWAP_LAYOUTS: {
3211                 struct file *file2;
3212                 struct lustre_swap_layouts lsl;
3213
3214                 if (copy_from_user(&lsl, (char __user *)arg,
3215                                    sizeof(struct lustre_swap_layouts)))
3216                         RETURN(-EFAULT);
3217
3218                 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3219                         RETURN(-EPERM);
3220
3221                 file2 = fget(lsl.sl_fd);
3222                 if (file2 == NULL)
3223                         RETURN(-EBADF);
3224
3225                 /* O_WRONLY or O_RDWR */
3226                 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3227                         GOTO(out, rc = -EPERM);
3228
3229                 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3230                         struct inode                    *inode2;
3231                         struct ll_inode_info            *lli;
3232                         struct obd_client_handle        *och = NULL;
3233
3234                         lli = ll_i2info(inode);
3235                         mutex_lock(&lli->lli_och_mutex);
3236                         if (fd->fd_lease_och != NULL) {
3237                                 och = fd->fd_lease_och;
3238                                 fd->fd_lease_och = NULL;
3239                         }
3240                         mutex_unlock(&lli->lli_och_mutex);
3241                         if (och == NULL)
3242                                 GOTO(out, rc = -ENOLCK);
3243                         inode2 = file_inode(file2);
3244                         rc = ll_swap_layouts_close(och, inode, inode2);
3245                 } else {
3246                         rc = ll_swap_layouts(file, file2, &lsl);
3247                 }
3248 out:
3249                 fput(file2);
3250                 RETURN(rc);
3251         }
3252         case LL_IOC_LOV_GETSTRIPE:
3253         case LL_IOC_LOV_GETSTRIPE_NEW:
3254                 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3255         case FSFILT_IOC_GETFLAGS:
3256         case FSFILT_IOC_SETFLAGS:
3257                 RETURN(ll_iocontrol(inode, file, cmd, arg));
3258         case FSFILT_IOC_GETVERSION_OLD:
3259         case FSFILT_IOC_GETVERSION:
3260                 RETURN(put_user(inode->i_generation, (int __user *)arg));
3261         case LL_IOC_GROUP_LOCK:
3262                 RETURN(ll_get_grouplock(inode, file, arg));
3263         case LL_IOC_GROUP_UNLOCK:
3264                 RETURN(ll_put_grouplock(inode, file, arg));
3265         case IOC_OBD_STATFS:
3266                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3267
3268         /* We need to special case any other ioctls we want to handle,
3269          * to send them to the MDS/OST as appropriate and to properly
3270          * network encode the arg field.
3271         case FSFILT_IOC_SETVERSION_OLD:
3272         case FSFILT_IOC_SETVERSION:
3273         */
3274         case LL_IOC_FLUSHCTX:
3275                 RETURN(ll_flush_ctx(inode));
3276         case LL_IOC_PATH2FID: {
3277                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3278                                  sizeof(struct lu_fid)))
3279                         RETURN(-EFAULT);
3280
3281                 RETURN(0);
3282         }
3283         case LL_IOC_GETPARENT:
3284                 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3285
3286         case OBD_IOC_FID2PATH:
3287                 RETURN(ll_fid2path(inode, (void __user *)arg));
3288         case LL_IOC_DATA_VERSION: {
3289                 struct ioc_data_version idv;
3290                 int rc;
3291
3292                 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3293                         RETURN(-EFAULT);
3294
3295                 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3296                 rc = ll_ioc_data_version(inode, &idv);
3297
3298                 if (rc == 0 &&
3299                     copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3300                         RETURN(-EFAULT);
3301
3302                 RETURN(rc);
3303         }
3304
3305         case LL_IOC_GET_MDTIDX: {
3306                 int mdtidx;
3307
3308                 mdtidx = ll_get_mdt_idx(inode);
3309                 if (mdtidx < 0)
3310                         RETURN(mdtidx);
3311
3312                 if (put_user((int)mdtidx, (int __user *)arg))
3313                         RETURN(-EFAULT);
3314
3315                 RETURN(0);
3316         }
3317         case OBD_IOC_GETDTNAME:
3318         case OBD_IOC_GETMDNAME:
3319                 RETURN(ll_get_obd_name(inode, cmd, arg));
3320         case LL_IOC_HSM_STATE_GET: {
3321                 struct md_op_data       *op_data;
3322                 struct hsm_user_state   *hus;
3323                 int                      rc;
3324
3325                 OBD_ALLOC_PTR(hus);
3326                 if (hus == NULL)
3327                         RETURN(-ENOMEM);
3328
3329                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3330                                              LUSTRE_OPC_ANY, hus);
3331                 if (IS_ERR(op_data)) {
3332                         OBD_FREE_PTR(hus);
3333                         RETURN(PTR_ERR(op_data));
3334                 }
3335
3336                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3337                                    op_data, NULL);
3338
3339                 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3340                         rc = -EFAULT;
3341
3342                 ll_finish_md_op_data(op_data);
3343                 OBD_FREE_PTR(hus);
3344                 RETURN(rc);
3345         }
3346         case LL_IOC_HSM_STATE_SET: {
3347                 struct hsm_state_set    *hss;
3348                 int                      rc;
3349
3350                 OBD_ALLOC_PTR(hss);
3351                 if (hss == NULL)
3352                         RETURN(-ENOMEM);
3353
3354                 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3355                         OBD_FREE_PTR(hss);
3356                         RETURN(-EFAULT);
3357                 }
3358
3359                 rc = ll_hsm_state_set(inode, hss);
3360
3361                 OBD_FREE_PTR(hss);
3362                 RETURN(rc);
3363         }
3364         case LL_IOC_HSM_ACTION: {
3365                 struct md_op_data               *op_data;
3366                 struct hsm_current_action       *hca;
3367                 int                              rc;
3368
3369                 OBD_ALLOC_PTR(hca);
3370                 if (hca == NULL)
3371                         RETURN(-ENOMEM);
3372
3373                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3374                                              LUSTRE_OPC_ANY, hca);
3375                 if (IS_ERR(op_data)) {
3376                         OBD_FREE_PTR(hca);
3377                         RETURN(PTR_ERR(op_data));
3378                 }
3379
3380                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3381                                    op_data, NULL);
3382
3383                 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3384                         rc = -EFAULT;
3385
3386                 ll_finish_md_op_data(op_data);
3387                 OBD_FREE_PTR(hca);
3388                 RETURN(rc);
3389         }
3390         case LL_IOC_SET_LEASE_OLD: {
3391                 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3392
3393                 RETURN(ll_file_set_lease(file, &ioc, 0));
3394         }
3395         case LL_IOC_SET_LEASE: {
3396                 struct ll_ioc_lease ioc;
3397
3398                 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3399                         RETURN(-EFAULT);
3400
3401                 RETURN(ll_file_set_lease(file, &ioc, arg));
3402         }
3403         case LL_IOC_GET_LEASE: {
3404                 struct ll_inode_info *lli = ll_i2info(inode);
3405                 struct ldlm_lock *lock = NULL;
3406                 fmode_t fmode = 0;
3407
3408                 mutex_lock(&lli->lli_och_mutex);
3409                 if (fd->fd_lease_och != NULL) {
3410                         struct obd_client_handle *och = fd->fd_lease_och;
3411
3412                         lock = ldlm_handle2lock(&och->och_lease_handle);
3413                         if (lock != NULL) {
3414                                 lock_res_and_lock(lock);
3415                                 if (!ldlm_is_cancel(lock))
3416                                         fmode = och->och_flags;
3417
3418                                 unlock_res_and_lock(lock);
3419                                 LDLM_LOCK_PUT(lock);
3420                         }
3421                 }
3422                 mutex_unlock(&lli->lli_och_mutex);
3423
3424                 RETURN(ll_lease_type_from_fmode(fmode));
3425         }
3426         case LL_IOC_HSM_IMPORT: {
3427                 struct hsm_user_import *hui;
3428
3429                 OBD_ALLOC_PTR(hui);
3430                 if (hui == NULL)
3431                         RETURN(-ENOMEM);
3432
3433                 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3434                         OBD_FREE_PTR(hui);
3435                         RETURN(-EFAULT);
3436                 }
3437
3438                 rc = ll_hsm_import(inode, file, hui);
3439
3440                 OBD_FREE_PTR(hui);
3441                 RETURN(rc);
3442         }
3443         case LL_IOC_FUTIMES_3: {
3444                 struct ll_futimes_3 lfu;
3445
3446                 if (copy_from_user(&lfu,
3447                                    (const struct ll_futimes_3 __user *)arg,
3448                                    sizeof(lfu)))
3449                         RETURN(-EFAULT);
3450
3451                 RETURN(ll_file_futimes_3(file, &lfu));
3452         }
3453         case LL_IOC_LADVISE: {
3454                 struct llapi_ladvise_hdr *k_ladvise_hdr;
3455                 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
3456                 int i;
3457                 int num_advise;
3458                 int alloc_size = sizeof(*k_ladvise_hdr);
3459
3460                 rc = 0;
3461                 u_ladvise_hdr = (void __user *)arg;
3462                 OBD_ALLOC_PTR(k_ladvise_hdr);
3463                 if (k_ladvise_hdr == NULL)
3464                         RETURN(-ENOMEM);
3465
3466                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3467                         GOTO(out_ladvise, rc = -EFAULT);
3468
3469                 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
3470                     k_ladvise_hdr->lah_count < 1)
3471                         GOTO(out_ladvise, rc = -EINVAL);
3472
3473                 num_advise = k_ladvise_hdr->lah_count;
3474                 if (num_advise >= LAH_COUNT_MAX)
3475                         GOTO(out_ladvise, rc = -EFBIG);
3476
3477                 OBD_FREE_PTR(k_ladvise_hdr);
3478                 alloc_size = offsetof(typeof(*k_ladvise_hdr),
3479                                       lah_advise[num_advise]);
3480                 OBD_ALLOC(k_ladvise_hdr, alloc_size);
3481                 if (k_ladvise_hdr == NULL)
3482                         RETURN(-ENOMEM);
3483
3484                 /*
3485                  * TODO: submit multiple advices to one server in a single RPC
3486                  */
3487                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3488                         GOTO(out_ladvise, rc = -EFAULT);
3489
3490                 for (i = 0; i < num_advise; i++) {
3491                         struct llapi_lu_ladvise *k_ladvise =
3492                                         &k_ladvise_hdr->lah_advise[i];
3493                         struct llapi_lu_ladvise __user *u_ladvise =
3494                                         &u_ladvise_hdr->lah_advise[i];
3495
3496                         rc = ll_ladvise_sanity(inode, k_ladvise);
3497                         if (rc)
3498                                 GOTO(out_ladvise, rc);
3499
3500                         switch (k_ladvise->lla_advice) {
3501                         case LU_LADVISE_LOCKNOEXPAND:
3502                                 rc = ll_lock_noexpand(file,
3503                                                k_ladvise->lla_peradvice_flags);
3504                                 GOTO(out_ladvise, rc);
3505                         case LU_LADVISE_LOCKAHEAD:
3506
3507                                 rc = ll_file_lock_ahead(file, k_ladvise);
3508
3509                                 if (rc < 0)
3510                                         GOTO(out_ladvise, rc);
3511
3512                                 if (put_user(rc,
3513                                              &u_ladvise->lla_lockahead_result))
3514                                         GOTO(out_ladvise, rc = -EFAULT);
3515                                 break;
3516                         default:
3517                                 rc = ll_ladvise(inode, file,
3518                                                 k_ladvise_hdr->lah_flags,
3519                                                 k_ladvise);
3520                                 if (rc)
3521                                         GOTO(out_ladvise, rc);
3522                                 break;
3523                         }
3524
3525                 }
3526
3527 out_ladvise:
3528                 OBD_FREE(k_ladvise_hdr, alloc_size);
3529                 RETURN(rc);
3530         }
3531         case LL_IOC_FLR_SET_MIRROR: {
3532                 /* mirror I/O must be direct to avoid polluting page cache
3533                  * by stale data. */
3534                 if (!(file->f_flags & O_DIRECT))
3535                         RETURN(-EINVAL);
3536
3537                 fd->fd_designated_mirror = (__u32)arg;
3538                 RETURN(0);
3539         }
3540         case LL_IOC_FSGETXATTR:
3541                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
3542         case LL_IOC_FSSETXATTR:
3543                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
3544         case BLKSSZGET:
3545                 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
3546         default:
3547                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
3548                                      (void __user *)arg));
3549         }
3550 }
3551
3552 #ifndef HAVE_FILE_LLSEEK_SIZE
3553 static inline loff_t
3554 llseek_execute(struct file *file, loff_t offset, loff_t maxsize)
3555 {
3556         if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
3557                 return -EINVAL;
3558         if (offset > maxsize)
3559                 return -EINVAL;
3560
3561         if (offset != file->f_pos) {
3562                 file->f_pos = offset;
3563                 file->f_version = 0;
3564         }
3565         return offset;
3566 }
3567
3568 static loff_t
3569 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
3570                 loff_t maxsize, loff_t eof)
3571 {
3572         struct inode *inode = file_inode(file);
3573
3574         switch (origin) {
3575         case SEEK_END:
3576                 offset += eof;
3577                 break;
3578         case SEEK_CUR:
3579                 /*
3580                  * Here we special-case the lseek(fd, 0, SEEK_CUR)
3581                  * position-querying operation.  Avoid rewriting the "same"
3582                  * f_pos value back to the file because a concurrent read(),
3583                  * write() or lseek() might have altered it
3584                  */
3585                 if (offset == 0)
3586                         return file->f_pos;
3587                 /*
3588                  * f_lock protects against read/modify/write race with other
3589                  * SEEK_CURs. Note that parallel writes and reads behave
3590                  * like SEEK_SET.
3591                  */
3592                 inode_lock(inode);
3593                 offset = llseek_execute(file, file->f_pos + offset, maxsize);
3594                 inode_unlock(inode);
3595                 return offset;
3596         case SEEK_DATA:
3597                 /*
3598                  * In the generic case the entire file is data, so as long as
3599                  * offset isn't at the end of the file then the offset is data.
3600                  */
3601                 if (offset >= eof)
3602                         return -ENXIO;
3603                 break;
3604         case SEEK_HOLE:
3605                 /*
3606                  * There is a virtual hole at the end of the file, so as long as
3607                  * offset isn't i_size or larger, return i_size.
3608                  */
3609                 if (offset >= eof)
3610                         return -ENXIO;
3611                 offset = eof;
3612                 break;
3613         }
3614
3615         return llseek_execute(file, offset, maxsize);
3616 }
3617 #endif
3618
3619 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
3620 {
3621         struct inode *inode = file_inode(file);
3622         loff_t retval, eof = 0;
3623
3624         ENTRY;
3625         retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
3626                            (origin == SEEK_CUR) ? file->f_pos : 0);
3627         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
3628                PFID(ll_inode2fid(inode)), inode, retval, retval,
3629                origin);
3630         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
3631
3632         if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
3633                 retval = ll_glimpse_size(inode);
3634                 if (retval != 0)
3635                         RETURN(retval);
3636                 eof = i_size_read(inode);
3637         }
3638
3639         retval = ll_generic_file_llseek_size(file, offset, origin,
3640                                           ll_file_maxbytes(inode), eof);
3641         RETURN(retval);
3642 }
3643
3644 static int ll_flush(struct file *file, fl_owner_t id)
3645 {
3646         struct inode *inode = file_inode(file);
3647         struct ll_inode_info *lli = ll_i2info(inode);
3648         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3649         int rc, err;
3650
3651         LASSERT(!S_ISDIR(inode->i_mode));
3652
3653         /* catch async errors that were recorded back when async writeback
3654          * failed for pages in this mapping. */
3655         rc = lli->lli_async_rc;
3656         lli->lli_async_rc = 0;
3657         if (lli->lli_clob != NULL) {
3658                 err = lov_read_and_clear_async_rc(lli->lli_clob);
3659                 if (rc == 0)
3660                         rc = err;
3661         }
3662
3663         /* The application has been told write failure already.
3664          * Do not report failure again. */
3665         if (fd->fd_write_failed)
3666                 return 0;
3667         return rc ? -EIO : 0;
3668 }
3669
3670 /**
3671  * Called to make sure a portion of file has been written out.
3672  * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
3673  *
3674  * Return how many pages have been written.
3675  */
3676 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
3677                        enum cl_fsync_mode mode, int ignore_layout)
3678 {
3679         struct lu_env *env;
3680         struct cl_io *io;
3681         struct cl_fsync_io *fio;
3682         int result;
3683         __u16 refcheck;
3684         ENTRY;
3685
3686         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
3687             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
3688                 RETURN(-EINVAL);
3689
3690         env = cl_env_get(&refcheck);
3691         if (IS_ERR(env))
3692                 RETURN(PTR_ERR(env));
3693
3694         io = vvp_env_thread_io(env);
3695         io->ci_obj = ll_i2info(inode)->lli_clob;
3696         io->ci_ignore_layout = ignore_layout;
3697
3698         /* initialize parameters for sync */
3699         fio = &io->u.ci_fsync;
3700         fio->fi_start = start;
3701         fio->fi_end = end;
3702         fio->fi_fid = ll_inode2fid(inode);
3703         fio->fi_mode = mode;
3704         fio->fi_nr_written = 0;
3705
3706         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
3707                 result = cl_io_loop(env, io);
3708         else
3709                 result = io->ci_result;
3710         if (result == 0)
3711                 result = fio->fi_nr_written;
3712         cl_io_fini(env, io);
3713         cl_env_put(env, &refcheck);
3714
3715         RETURN(result);
3716 }
3717
3718 /*
3719  * When dentry is provided (the 'else' case), file_dentry() may be
3720  * null and dentry must be used directly rather than pulled from
3721  * file_dentry() as is done otherwise.
3722  */
3723
3724 #ifdef HAVE_FILE_FSYNC_4ARGS
3725 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3726 {
3727         struct dentry *dentry = file_dentry(file);
3728         bool lock_inode;
3729 #elif defined(HAVE_FILE_FSYNC_2ARGS)
3730 int ll_fsync(struct file *file, int datasync)
3731 {
3732         struct dentry *dentry = file_dentry(file);
3733         loff_t start = 0;
3734         loff_t end = LLONG_MAX;
3735 #else
3736 int ll_fsync(struct file *file, struct dentry *dentry, int datasync)
3737 {
3738         loff_t start = 0;
3739         loff_t end = LLONG_MAX;
3740 #endif
3741         struct inode *inode = dentry->d_inode;
3742         struct ll_inode_info *lli = ll_i2info(inode);
3743         struct ptlrpc_request *req;
3744         int rc, err;
3745         ENTRY;
3746
3747         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
3748                PFID(ll_inode2fid(inode)), inode);
3749         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
3750
3751 #ifdef HAVE_FILE_FSYNC_4ARGS
3752         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
3753         lock_inode = !lli->lli_inode_locked;
3754         if (lock_inode)
3755                 inode_lock(inode);
3756 #else
3757         /* fsync's caller has already called _fdata{sync,write}, we want
3758          * that IO to finish before calling the osc and mdc sync methods */
3759         rc = filemap_fdatawait(inode->i_mapping);
3760 #endif
3761
3762         /* catch async errors that were recorded back when async writeback
3763          * failed for pages in this mapping. */
3764         if (!S_ISDIR(inode->i_mode)) {
3765                 err = lli->lli_async_rc;
3766                 lli->lli_async_rc = 0;
3767                 if (rc == 0)
3768                         rc = err;
3769                 if (lli->lli_clob != NULL) {
3770                         err = lov_read_and_clear_async_rc(lli->lli_clob);
3771                         if (rc == 0)
3772                                 rc = err;
3773                 }
3774         }
3775
3776         err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
3777         if (!rc)
3778                 rc = err;
3779         if (!err)
3780                 ptlrpc_req_finished(req);
3781
3782         if (S_ISREG(inode->i_mode)) {
3783                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3784
3785                 err = cl_sync_file_range(inode, start, end, CL_FSYNC_ALL, 0);
3786                 if (rc == 0 && err < 0)
3787                         rc = err;
3788                 if (rc < 0)
3789                         fd->fd_write_failed = true;
3790                 else
3791                         fd->fd_write_failed = false;
3792         }
3793
3794 #ifdef HAVE_FILE_FSYNC_4ARGS
3795         if (lock_inode)
3796                 inode_unlock(inode);
3797 #endif
3798         RETURN(rc);
3799 }
3800
3801 static int
3802 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
3803 {
3804         struct inode *inode = file_inode(file);
3805         struct ll_sb_info *sbi = ll_i2sbi(inode);
3806         struct ldlm_enqueue_info einfo = {
3807                 .ei_type        = LDLM_FLOCK,
3808                 .ei_cb_cp       = ldlm_flock_completion_ast,
3809                 .ei_cbdata      = file_lock,
3810         };
3811         struct md_op_data *op_data;
3812         struct lustre_handle lockh = { 0 };
3813         union ldlm_policy_data flock = { { 0 } };
3814         int fl_type = file_lock->fl_type;
3815         __u64 flags = 0;
3816         int rc;
3817         int rc2 = 0;
3818         ENTRY;
3819
3820         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
3821                PFID(ll_inode2fid(inode)), file_lock);
3822
3823         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
3824
3825         if (file_lock->fl_flags & FL_FLOCK) {
3826                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
3827                 /* flocks are whole-file locks */
3828                 flock.l_flock.end = OFFSET_MAX;
3829                 /* For flocks owner is determined by the local file desctiptor*/
3830                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
3831         } else if (file_lock->fl_flags & FL_POSIX) {
3832                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
3833                 flock.l_flock.start = file_lock->fl_start;
3834                 flock.l_flock.end = file_lock->fl_end;
3835         } else {
3836                 RETURN(-EINVAL);
3837         }
3838         flock.l_flock.pid = file_lock->fl_pid;
3839
3840         /* Somewhat ugly workaround for svc lockd.
3841          * lockd installs custom fl_lmops->lm_compare_owner that checks
3842          * for the fl_owner to be the same (which it always is on local node
3843          * I guess between lockd processes) and then compares pid.
3844          * As such we assign pid to the owner field to make it all work,
3845          * conflict with normal locks is unlikely since pid space and
3846          * pointer space for current->files are not intersecting */
3847         if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
3848                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
3849
3850         switch (fl_type) {
3851         case F_RDLCK:
3852                 einfo.ei_mode = LCK_PR;
3853                 break;
3854         case F_UNLCK:
3855                 /* An unlock request may or may not have any relation to
3856                  * existing locks so we may not be able to pass a lock handle
3857                  * via a normal ldlm_lock_cancel() request. The request may even
3858                  * unlock a byte range in the middle of an existing lock. In
3859                  * order to process an unlock request we need all of the same
3860                  * information that is given with a normal read or write record
3861                  * lock request. To avoid creating another ldlm unlock (cancel)
3862                  * message we'll treat a LCK_NL flock request as an unlock. */
3863                 einfo.ei_mode = LCK_NL;
3864                 break;
3865         case F_WRLCK:
3866                 einfo.ei_mode = LCK_PW;
3867                 break;
3868         default:
3869                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
3870                 RETURN (-ENOTSUPP);
3871         }
3872
3873         switch (cmd) {
3874         case F_SETLKW:
3875 #ifdef F_SETLKW64
3876         case F_SETLKW64:
3877 #endif
3878                 flags = 0;
3879                 break;
3880         case F_SETLK:
3881 #ifdef F_SETLK64
3882         case F_SETLK64:
3883 #endif
3884                 flags = LDLM_FL_BLOCK_NOWAIT;
3885                 break;
3886         case F_GETLK:
3887 #ifdef F_GETLK64
3888         case F_GETLK64:
3889 #endif
3890                 flags = LDLM_FL_TEST_LOCK;
3891                 break;
3892         default:
3893                 CERROR("unknown fcntl lock command: %d\n", cmd);
3894                 RETURN (-EINVAL);
3895         }
3896
3897         /* Save the old mode so that if the mode in the lock changes we
3898          * can decrement the appropriate reader or writer refcount. */
3899         file_lock->fl_type = einfo.ei_mode;
3900
3901         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3902                                      LUSTRE_OPC_ANY, NULL);
3903         if (IS_ERR(op_data))
3904                 RETURN(PTR_ERR(op_data));
3905
3906         CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
3907                "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
3908                flock.l_flock.pid, flags, einfo.ei_mode,
3909                flock.l_flock.start, flock.l_flock.end);
3910
3911         rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
3912                         flags);
3913
3914         /* Restore the file lock type if not TEST lock. */
3915         if (!(flags & LDLM_FL_TEST_LOCK))
3916                 file_lock->fl_type = fl_type;
3917
3918 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
3919         if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
3920             !(flags & LDLM_FL_TEST_LOCK))
3921                 rc2  = locks_lock_file_wait(file, file_lock);
3922 #else
3923         if ((file_lock->fl_flags & FL_FLOCK) &&
3924             (rc == 0 || file_lock->fl_type == F_UNLCK))
3925                 rc2  = flock_lock_file_wait(file, file_lock);
3926         if ((file_lock->fl_flags & FL_POSIX) &&
3927             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
3928             !(flags & LDLM_FL_TEST_LOCK))
3929                 rc2  = posix_lock_file_wait(file, file_lock);
3930 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
3931
3932         if (rc2 && file_lock->fl_type != F_UNLCK) {
3933                 einfo.ei_mode = LCK_NL;
3934                 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
3935                            &lockh, flags);
3936                 rc = rc2;
3937         }
3938
3939         ll_finish_md_op_data(op_data);
3940
3941         RETURN(rc);
3942 }
3943
3944 int ll_get_fid_by_name(struct inode *parent, const char *name,
3945                        int namelen, struct lu_fid *fid,
3946                        struct inode **inode)
3947 {
3948         struct md_op_data       *op_data = NULL;
3949         struct mdt_body         *body;
3950         struct ptlrpc_request   *req;
3951         int                     rc;
3952         ENTRY;
3953
3954         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
3955                                      LUSTRE_OPC_ANY, NULL);
3956         if (IS_ERR(op_data))
3957                 RETURN(PTR_ERR(op_data));
3958
3959         op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
3960         rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
3961         ll_finish_md_op_data(op_data);
3962         if (rc < 0)
3963                 RETURN(rc);
3964
3965         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3966         if (body == NULL)
3967                 GOTO(out_req, rc = -EFAULT);
3968         if (fid != NULL)
3969                 *fid = body->mbo_fid1;
3970
3971         if (inode != NULL)
3972                 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
3973 out_req:
3974         ptlrpc_req_finished(req);
3975         RETURN(rc);
3976 }
3977
3978 int ll_migrate(struct inode *parent, struct file *file, int mdtidx,
3979                const char *name, int namelen)
3980 {
3981         struct dentry         *dchild = NULL;
3982         struct inode          *child_inode = NULL;
3983         struct md_op_data     *op_data;
3984         struct ptlrpc_request *request = NULL;
3985         struct obd_client_handle *och = NULL;
3986         struct qstr           qstr;
3987         struct mdt_body         *body;
3988         int                    rc;
3989         __u64                   data_version = 0;
3990         ENTRY;
3991
3992         CDEBUG(D_VFSTRACE, "migrate %s under "DFID" to MDT%04x\n",
3993                name, PFID(ll_inode2fid(parent)), mdtidx);
3994
3995         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
3996                                      0, LUSTRE_OPC_ANY, NULL);
3997         if (IS_ERR(op_data))
3998                 RETURN(PTR_ERR(op_data));
3999
4000         /* Get child FID first */
4001         qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4002         qstr.name = name;
4003         qstr.len = namelen;
4004         dchild = d_lookup(file_dentry(file), &qstr);
4005         if (dchild != NULL) {
4006                 if (dchild->d_inode != NULL)
4007                         child_inode = igrab(dchild->d_inode);
4008                 dput(dchild);
4009         }
4010
4011         if (child_inode == NULL) {
4012                 rc = ll_get_fid_by_name(parent, name, namelen,
4013                                         &op_data->op_fid3, &child_inode);
4014                 if (rc != 0)
4015                         GOTO(out_free, rc);
4016         }
4017
4018         if (child_inode == NULL)
4019                 GOTO(out_free, rc = -EINVAL);
4020
4021         /*
4022          * lfs migrate command needs to be blocked on the client
4023          * by checking the migrate FID against the FID of the
4024          * filesystem root.
4025          */
4026         if (child_inode == parent->i_sb->s_root->d_inode)
4027                 GOTO(out_iput, rc = -EINVAL);
4028
4029         inode_lock(child_inode);
4030         op_data->op_fid3 = *ll_inode2fid(child_inode);
4031         if (!fid_is_sane(&op_data->op_fid3)) {
4032                 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4033                        ll_get_fsname(parent->i_sb, NULL, 0), name,
4034                        PFID(&op_data->op_fid3));
4035                 GOTO(out_unlock, rc = -EINVAL);
4036         }
4037
4038         rc = ll_get_mdt_idx_by_fid(ll_i2sbi(parent), &op_data->op_fid3);
4039         if (rc < 0)
4040                 GOTO(out_unlock, rc);
4041
4042         if (rc == mdtidx) {
4043                 CDEBUG(D_INFO, "%s: "DFID" is already on MDT%04x\n", name,
4044                        PFID(&op_data->op_fid3), mdtidx);
4045                 GOTO(out_unlock, rc = 0);
4046         }
4047 again:
4048         if (S_ISREG(child_inode->i_mode)) {
4049                 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4050                 if (IS_ERR(och)) {
4051                         rc = PTR_ERR(och);
4052                         och = NULL;
4053                         GOTO(out_unlock, rc);
4054                 }
4055
4056                 rc = ll_data_version(child_inode, &data_version,
4057                                      LL_DV_WR_FLUSH);
4058                 if (rc != 0)
4059                         GOTO(out_close, rc);
4060
4061                 op_data->op_handle = och->och_fh;
4062                 op_data->op_data = och->och_mod;
4063                 op_data->op_data_version = data_version;
4064                 op_data->op_lease_handle = och->och_lease_handle;
4065                 op_data->op_bias |= MDS_RENAME_MIGRATE;
4066         }
4067
4068         op_data->op_mds = mdtidx;
4069         op_data->op_cli_flags = CLI_MIGRATE;
4070         rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name,
4071                        namelen, name, namelen, &request);
4072         if (rc == 0) {
4073                 LASSERT(request != NULL);
4074                 ll_update_times(request, parent);
4075
4076                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4077                 LASSERT(body != NULL);
4078
4079                 /* If the server does release layout lock, then we cleanup
4080                  * the client och here, otherwise release it in out_close: */
4081                 if (och != NULL &&
4082                     body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4083                         obd_mod_put(och->och_mod);
4084                         md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4085                                                   och);
4086                         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
4087                         OBD_FREE_PTR(och);
4088                         och = NULL;
4089                 }
4090         }
4091
4092         if (request != NULL) {
4093                 ptlrpc_req_finished(request);
4094                 request = NULL;
4095         }
4096
4097         /* Try again if the file layout has changed. */
4098         if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4099                 goto again;
4100
4101 out_close:
4102         if (och != NULL) /* close the file */
4103                 ll_lease_close(och, child_inode, NULL);
4104         if (rc == 0)
4105                 clear_nlink(child_inode);
4106 out_unlock:
4107         inode_unlock(child_inode);
4108 out_iput:
4109         iput(child_inode);
4110 out_free:
4111         ll_finish_md_op_data(op_data);
4112         RETURN(rc);
4113 }
4114
4115 static int
4116 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4117 {
4118         ENTRY;
4119
4120         RETURN(-ENOSYS);
4121 }
4122
4123 /**
4124  * test if some locks matching bits and l_req_mode are acquired
4125  * - bits can be in different locks
4126  * - if found clear the common lock bits in *bits
4127  * - the bits not found, are kept in *bits
4128  * \param inode [IN]
4129  * \param bits [IN] searched lock bits [IN]
4130  * \param l_req_mode [IN] searched lock mode
4131  * \retval boolean, true iff all bits are found
4132  */
4133 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4134 {
4135         struct lustre_handle lockh;
4136         union ldlm_policy_data policy;
4137         enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4138                               (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4139         struct lu_fid *fid;
4140         __u64 flags;
4141         int i;
4142         ENTRY;
4143
4144         if (!inode)
4145                RETURN(0);
4146
4147         fid = &ll_i2info(inode)->lli_fid;
4148         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4149                ldlm_lockname[mode]);
4150
4151         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4152         for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
4153                 policy.l_inodebits.bits = *bits & (1 << i);
4154                 if (policy.l_inodebits.bits == 0)
4155                         continue;
4156
4157                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4158                                   &policy, mode, &lockh)) {
4159                         struct ldlm_lock *lock;
4160
4161                         lock = ldlm_handle2lock(&lockh);
4162                         if (lock) {
4163                                 *bits &=
4164                                       ~(lock->l_policy_data.l_inodebits.bits);
4165                                 LDLM_LOCK_PUT(lock);
4166                         } else {
4167                                 *bits &= ~policy.l_inodebits.bits;
4168                         }
4169                 }
4170         }
4171         RETURN(*bits == 0);
4172 }
4173
4174 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4175                                struct lustre_handle *lockh, __u64 flags,
4176                                enum ldlm_mode mode)
4177 {
4178         union ldlm_policy_data policy = { .l_inodebits = { bits } };
4179         struct lu_fid *fid;
4180         enum ldlm_mode rc;
4181         ENTRY;
4182
4183         fid = &ll_i2info(inode)->lli_fid;
4184         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4185
4186         rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4187                            fid, LDLM_IBITS, &policy, mode, lockh);
4188
4189         RETURN(rc);
4190 }
4191
4192 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4193 {
4194         /* Already unlinked. Just update nlink and return success */
4195         if (rc == -ENOENT) {
4196                 clear_nlink(inode);
4197                 /* If it is striped directory, and there is bad stripe
4198                  * Let's revalidate the dentry again, instead of returning
4199                  * error */
4200                 if (S_ISDIR(inode->i_mode) &&
4201                     ll_i2info(inode)->lli_lsm_md != NULL)
4202                         return 0;
4203
4204                 /* This path cannot be hit for regular files unless in
4205                  * case of obscure races, so no need to to validate
4206                  * size. */
4207                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4208                         return 0;
4209         } else if (rc != 0) {
4210                 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4211                              "%s: revalidate FID "DFID" error: rc = %d\n",
4212                              ll_get_fsname(inode->i_sb, NULL, 0),
4213                              PFID(ll_inode2fid(inode)), rc);
4214         }
4215
4216         return rc;
4217 }
4218
4219 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4220 {
4221         struct inode *inode = dentry->d_inode;
4222         struct obd_export *exp = ll_i2mdexp(inode);
4223         struct lookup_intent oit = {
4224                 .it_op = op,
4225         };
4226         struct ptlrpc_request *req = NULL;
4227         struct md_op_data *op_data;
4228         int rc = 0;
4229         ENTRY;
4230
4231         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4232                PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4233
4234         /* Call getattr by fid, so do not provide name at all. */
4235         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
4236                                      LUSTRE_OPC_ANY, NULL);
4237         if (IS_ERR(op_data))
4238                 RETURN(PTR_ERR(op_data));
4239
4240         rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4241         ll_finish_md_op_data(op_data);
4242         if (rc < 0) {
4243                 rc = ll_inode_revalidate_fini(inode, rc);
4244                 GOTO(out, rc);
4245         }
4246
4247         rc = ll_revalidate_it_finish(req, &oit, dentry);
4248         if (rc != 0) {
4249                 ll_intent_release(&oit);
4250                 GOTO(out, rc);
4251         }
4252
4253         /* Unlinked? Unhash dentry, so it is not picked up later by
4254          * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4255          * here to preserve get_cwd functionality on 2.6.
4256          * Bug 10503 */
4257         if (!dentry->d_inode->i_nlink) {
4258                 ll_lock_dcache(inode);
4259                 d_lustre_invalidate(dentry, 0);
4260                 ll_unlock_dcache(inode);
4261         }
4262
4263         ll_lookup_finish_locks(&oit, dentry);
4264 out:
4265         ptlrpc_req_finished(req);
4266
4267         return rc;
4268 }
4269
4270 static int ll_merge_md_attr(struct inode *inode)
4271 {
4272         struct cl_attr attr = { 0 };
4273         int rc;
4274
4275         LASSERT(ll_i2info(inode)->lli_lsm_md != NULL);
4276         rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4277                            &attr, ll_md_blocking_ast);
4278         if (rc != 0)
4279                 RETURN(rc);
4280
4281         set_nlink(inode, attr.cat_nlink);
4282         inode->i_blocks = attr.cat_blocks;
4283         i_size_write(inode, attr.cat_size);
4284
4285         ll_i2info(inode)->lli_atime = attr.cat_atime;
4286         ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4287         ll_i2info(inode)->lli_ctime = attr.cat_ctime;
4288
4289         RETURN(0);
4290 }
4291
4292 static inline dev_t ll_compat_encode_dev(dev_t dev)
4293 {
4294         /* The compat_sys_*stat*() syscalls will fail unless the
4295          * device majors and minors are both less than 256. Note that
4296          * the value returned here will be passed through
4297          * old_encode_dev() in cp_compat_stat(). And so we are not
4298          * trying to return a valid compat (u16) device number, just
4299          * one that will pass the old_valid_dev() check. */
4300
4301         return MKDEV(MAJOR(dev) & 0xff, MINOR(dev) & 0xff);
4302 }
4303
4304 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
4305 int ll_getattr(const struct path *path, struct kstat *stat,
4306                u32 request_mask, unsigned int flags)
4307 {
4308         struct dentry *de = path->dentry;
4309 #else
4310 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
4311 {
4312 #endif
4313         struct inode *inode = de->d_inode;
4314         struct ll_sb_info *sbi = ll_i2sbi(inode);
4315         struct ll_inode_info *lli = ll_i2info(inode);
4316         int rc;
4317
4318         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
4319
4320         rc = ll_inode_revalidate(de, IT_GETATTR);
4321         if (rc < 0)
4322                 RETURN(rc);
4323
4324         if (S_ISREG(inode->i_mode)) {
4325                 /* In case of restore, the MDT has the right size and has
4326                  * already send it back without granting the layout lock,
4327                  * inode is up-to-date so glimpse is useless.
4328                  * Also to glimpse we need the layout, in case of a running
4329                  * restore the MDT holds the layout lock so the glimpse will
4330                  * block up to the end of restore (getattr will block)
4331                  */
4332                 if (!ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
4333                         rc = ll_glimpse_size(inode);
4334                         if (rc < 0)
4335                                 RETURN(rc);
4336                 }
4337         } else {
4338                 /* If object isn't regular a file then don't validate size. */
4339                 if (S_ISDIR(inode->i_mode) &&
4340                     lli->lli_lsm_md != NULL) {
4341                         rc = ll_merge_md_attr(inode);
4342                         if (rc < 0)
4343                                 RETURN(rc);
4344                 }
4345
4346                 LTIME_S(inode->i_atime) = lli->lli_atime;
4347                 LTIME_S(inode->i_mtime) = lli->lli_mtime;
4348                 LTIME_S(inode->i_ctime) = lli->lli_ctime;
4349         }
4350
4351         OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
4352
4353         if (ll_need_32bit_api(sbi)) {
4354                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
4355                 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
4356                 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
4357         } else {
4358                 stat->ino = inode->i_ino;
4359                 stat->dev = inode->i_sb->s_dev;
4360                 stat->rdev = inode->i_rdev;
4361         }
4362
4363         stat->mode = inode->i_mode;
4364         stat->uid = inode->i_uid;
4365         stat->gid = inode->i_gid;
4366         stat->atime = inode->i_atime;
4367         stat->mtime = inode->i_mtime;
4368         stat->ctime = inode->i_ctime;
4369         stat->blksize = sbi->ll_stat_blksize ?: 1 << inode->i_blkbits;
4370
4371         stat->nlink = inode->i_nlink;
4372         stat->size = i_size_read(inode);
4373         stat->blocks = inode->i_blocks;
4374
4375         return 0;
4376 }
4377
4378 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4379                      __u64 start, __u64 len)
4380 {
4381         int             rc;
4382         size_t          num_bytes;
4383         struct fiemap   *fiemap;
4384         unsigned int    extent_count = fieinfo->fi_extents_max;
4385
4386         num_bytes = sizeof(*fiemap) + (extent_count *
4387                                        sizeof(struct fiemap_extent));
4388         OBD_ALLOC_LARGE(fiemap, num_bytes);
4389
4390         if (fiemap == NULL)
4391                 RETURN(-ENOMEM);
4392
4393         fiemap->fm_flags = fieinfo->fi_flags;
4394         fiemap->fm_extent_count = fieinfo->fi_extents_max;
4395         fiemap->fm_start = start;
4396         fiemap->fm_length = len;
4397         if (extent_count > 0 &&
4398             copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
4399                            sizeof(struct fiemap_extent)) != 0)
4400                 GOTO(out, rc = -EFAULT);
4401
4402         rc = ll_do_fiemap(inode, fiemap, num_bytes);
4403
4404         fieinfo->fi_flags = fiemap->fm_flags;
4405         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
4406         if (extent_count > 0 &&
4407             copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
4408                          fiemap->fm_mapped_extents *
4409                          sizeof(struct fiemap_extent)) != 0)
4410                 GOTO(out, rc = -EFAULT);
4411 out:
4412         OBD_FREE_LARGE(fiemap, num_bytes);
4413         return rc;
4414 }
4415
4416 struct posix_acl *ll_get_acl(struct inode *inode, int type)
4417 {
4418         struct ll_inode_info *lli = ll_i2info(inode);
4419         struct posix_acl *acl = NULL;
4420         ENTRY;
4421
4422         spin_lock(&lli->lli_lock);
4423         /* VFS' acl_permission_check->check_acl will release the refcount */
4424         acl = posix_acl_dup(lli->lli_posix_acl);
4425         spin_unlock(&lli->lli_lock);
4426
4427         RETURN(acl);
4428 }
4429
4430 #ifdef HAVE_IOP_SET_ACL
4431 #ifdef CONFIG_FS_POSIX_ACL
4432 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
4433 {
4434         const char *name = NULL;
4435         char *value = NULL;
4436         size_t size = 0;
4437         int rc = 0;
4438         ENTRY;
4439
4440         switch (type) {
4441         case ACL_TYPE_ACCESS:
4442                 if (acl) {
4443                         rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
4444                         if (rc)
4445                                 GOTO(out, rc);
4446                 }
4447                 name = XATTR_NAME_POSIX_ACL_ACCESS;
4448                 break;
4449         case ACL_TYPE_DEFAULT:
4450                 if (!S_ISDIR(inode->i_mode))
4451                         GOTO(out, rc = acl ? -EACCES : 0);
4452                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
4453                 break;
4454         default:
4455                 GOTO(out, rc = -EINVAL);
4456         }
4457
4458         if (acl) {
4459                 size = posix_acl_xattr_size(acl->a_count);
4460                 value = kmalloc(size, GFP_NOFS);
4461                 if (value == NULL)
4462                         GOTO(out, rc = -ENOMEM);
4463
4464                 rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
4465                 if (rc < 0)
4466                         GOTO(out_free, rc);
4467         }
4468
4469         /* dentry is only used for *.lov attributes so it's safe to be NULL */
4470         rc = __vfs_setxattr(NULL, inode, name, value, size, XATTR_CREATE);
4471 out_free:
4472         kfree(value);
4473 out:
4474         if (!rc)
4475                 set_cached_acl(inode, type, acl);
4476         else
4477                 forget_cached_acl(inode, type);
4478         RETURN(rc);
4479 }
4480 #endif /* CONFIG_FS_POSIX_ACL */
4481 #endif /* HAVE_IOP_SET_ACL */
4482
4483 #ifndef HAVE_GENERIC_PERMISSION_2ARGS
4484 static int
4485 # ifdef HAVE_GENERIC_PERMISSION_4ARGS
4486 ll_check_acl(struct inode *inode, int mask, unsigned int flags)
4487 # else
4488 ll_check_acl(struct inode *inode, int mask)
4489 # endif
4490 {
4491 # ifdef CONFIG_FS_POSIX_ACL
4492         struct posix_acl *acl;
4493         int rc;
4494         ENTRY;
4495
4496 #  ifdef HAVE_GENERIC_PERMISSION_4ARGS
4497         if (flags & IPERM_FLAG_RCU)
4498                 return -ECHILD;
4499 #  endif
4500         acl = ll_get_acl(inode, ACL_TYPE_ACCESS);
4501
4502         if (!acl)
4503                 RETURN(-EAGAIN);
4504
4505         rc = posix_acl_permission(inode, acl, mask);
4506         posix_acl_release(acl);
4507
4508         RETURN(rc);
4509 # else /* !CONFIG_FS_POSIX_ACL */
4510         return -EAGAIN;
4511 # endif /* CONFIG_FS_POSIX_ACL */
4512 }
4513 #endif /* HAVE_GENERIC_PERMISSION_2ARGS */
4514
4515 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
4516 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
4517 #else
4518 # ifdef HAVE_INODE_PERMISION_2ARGS
4519 int ll_inode_permission(struct inode *inode, int mask)
4520 # else
4521 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
4522 # endif
4523 #endif
4524 {
4525         int rc = 0;
4526         struct ll_sb_info *sbi;
4527         struct root_squash_info *squash;
4528         struct cred *cred = NULL;
4529         const struct cred *old_cred = NULL;
4530         cfs_cap_t cap;
4531         bool squash_id = false;
4532         ENTRY;
4533
4534 #ifdef MAY_NOT_BLOCK
4535         if (mask & MAY_NOT_BLOCK)
4536                 return -ECHILD;
4537 #elif defined(HAVE_GENERIC_PERMISSION_4ARGS)
4538         if (flags & IPERM_FLAG_RCU)
4539                 return -ECHILD;
4540 #endif
4541
4542        /* as root inode are NOT getting validated in lookup operation,
4543         * need to do it before permission check. */
4544
4545         if (inode == inode->i_sb->s_root->d_inode) {
4546                 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
4547                 if (rc)
4548                         RETURN(rc);
4549         }
4550
4551         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
4552                PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
4553
4554         /* squash fsuid/fsgid if needed */
4555         sbi = ll_i2sbi(inode);
4556         squash = &sbi->ll_squash;
4557         if (unlikely(squash->rsi_uid != 0 &&
4558                      uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
4559                      !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
4560                         squash_id = true;
4561         }
4562         if (squash_id) {
4563                 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
4564                        __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
4565                        squash->rsi_uid, squash->rsi_gid);
4566
4567                 /* update current process's credentials
4568                  * and FS capability */
4569                 cred = prepare_creds();
4570                 if (cred == NULL)
4571                         RETURN(-ENOMEM);
4572
4573                 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
4574                 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
4575                 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
4576                         if ((1 << cap) & CFS_CAP_FS_MASK)
4577                                 cap_lower(cred->cap_effective, cap);
4578                 }
4579                 old_cred = override_creds(cred);
4580         }
4581
4582         ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM, 1);
4583         rc = ll_generic_permission(inode, mask, flags, ll_check_acl);
4584         /* restore current process's credentials and FS capability */
4585         if (squash_id) {
4586                 revert_creds(old_cred);
4587                 put_cred(cred);
4588         }
4589
4590         RETURN(rc);
4591 }
4592
4593 /* -o localflock - only provides locally consistent flock locks */
4594 struct file_operations ll_file_operations = {
4595 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4596 # ifdef HAVE_SYNC_READ_WRITE
4597         .read           = new_sync_read,
4598         .write          = new_sync_write,
4599 # endif
4600         .read_iter      = ll_file_read_iter,
4601         .write_iter     = ll_file_write_iter,
4602 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4603         .read           = ll_file_read,
4604         .aio_read       = ll_file_aio_read,
4605         .write          = ll_file_write,
4606         .aio_write      = ll_file_aio_write,
4607 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4608         .unlocked_ioctl = ll_file_ioctl,
4609         .open           = ll_file_open,
4610         .release        = ll_file_release,
4611         .mmap           = ll_file_mmap,
4612         .llseek         = ll_file_seek,
4613         .splice_read    = ll_file_splice_read,
4614         .fsync          = ll_fsync,
4615         .flush          = ll_flush
4616 };
4617
4618 struct file_operations ll_file_operations_flock = {
4619 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4620 # ifdef HAVE_SYNC_READ_WRITE
4621         .read           = new_sync_read,
4622         .write          = new_sync_write,
4623 # endif /* HAVE_SYNC_READ_WRITE */
4624         .read_iter      = ll_file_read_iter,
4625         .write_iter     = ll_file_write_iter,
4626 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4627         .read           = ll_file_read,
4628         .aio_read       = ll_file_aio_read,
4629         .write          = ll_file_write,
4630         .aio_write      = ll_file_aio_write,
4631 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4632         .unlocked_ioctl = ll_file_ioctl,
4633         .open           = ll_file_open,
4634         .release        = ll_file_release,
4635         .mmap           = ll_file_mmap,
4636         .llseek         = ll_file_seek,
4637         .splice_read    = ll_file_splice_read,
4638         .fsync          = ll_fsync,
4639         .flush          = ll_flush,
4640         .flock          = ll_file_flock,
4641         .lock           = ll_file_flock
4642 };
4643
4644 /* These are for -o noflock - to return ENOSYS on flock calls */
4645 struct file_operations ll_file_operations_noflock = {
4646 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
4647 # ifdef HAVE_SYNC_READ_WRITE
4648         .read           = new_sync_read,
4649         .write          = new_sync_write,
4650 # endif /* HAVE_SYNC_READ_WRITE */
4651         .read_iter      = ll_file_read_iter,
4652         .write_iter     = ll_file_write_iter,
4653 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4654         .read           = ll_file_read,
4655         .aio_read       = ll_file_aio_read,
4656         .write          = ll_file_write,
4657         .aio_write      = ll_file_aio_write,
4658 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
4659         .unlocked_ioctl = ll_file_ioctl,
4660         .open           = ll_file_open,
4661         .release        = ll_file_release,
4662         .mmap           = ll_file_mmap,
4663         .llseek         = ll_file_seek,
4664         .splice_read    = ll_file_splice_read,
4665         .fsync          = ll_fsync,
4666         .flush          = ll_flush,
4667         .flock          = ll_file_noflock,
4668         .lock           = ll_file_noflock
4669 };
4670
4671 struct inode_operations ll_file_inode_operations = {
4672         .setattr        = ll_setattr,
4673         .getattr        = ll_getattr,
4674         .permission     = ll_inode_permission,
4675 #ifdef HAVE_IOP_XATTR
4676         .setxattr       = ll_setxattr,
4677         .getxattr       = ll_getxattr,
4678         .removexattr    = ll_removexattr,
4679 #endif
4680         .listxattr      = ll_listxattr,
4681         .fiemap         = ll_fiemap,
4682 #ifdef HAVE_IOP_GET_ACL
4683         .get_acl        = ll_get_acl,
4684 #endif
4685 #ifdef HAVE_IOP_SET_ACL
4686         .set_acl        = ll_set_acl,
4687 #endif
4688 };
4689
4690 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
4691 {
4692         struct ll_inode_info *lli = ll_i2info(inode);
4693         struct cl_object *obj = lli->lli_clob;
4694         struct lu_env *env;
4695         int rc;
4696         __u16 refcheck;
4697         ENTRY;
4698
4699         if (obj == NULL)
4700                 RETURN(0);
4701
4702         env = cl_env_get(&refcheck);
4703         if (IS_ERR(env))
4704                 RETURN(PTR_ERR(env));
4705
4706         rc = cl_conf_set(env, lli->lli_clob, conf);
4707         if (rc < 0)
4708                 GOTO(out, rc);
4709
4710         if (conf->coc_opc == OBJECT_CONF_SET) {
4711                 struct ldlm_lock *lock = conf->coc_lock;
4712                 struct cl_layout cl = {
4713                         .cl_layout_gen = 0,
4714                 };
4715
4716                 LASSERT(lock != NULL);
4717                 LASSERT(ldlm_has_layout(lock));
4718
4719                 /* it can only be allowed to match after layout is
4720                  * applied to inode otherwise false layout would be
4721                  * seen. Applying layout shoud happen before dropping
4722                  * the intent lock. */
4723                 ldlm_lock_allow_match(lock);
4724
4725                 rc = cl_object_layout_get(env, obj, &cl);
4726                 if (rc < 0)
4727                         GOTO(out, rc);
4728
4729                 CDEBUG(D_VFSTRACE,
4730                        DFID": layout version change: %u -> %u\n",
4731                        PFID(&lli->lli_fid), ll_layout_version_get(lli),
4732                        cl.cl_layout_gen);
4733                 ll_layout_version_set(lli, cl.cl_layout_gen);
4734         }
4735
4736 out:
4737         cl_env_put(env, &refcheck);
4738
4739         RETURN(rc);
4740 }
4741
4742 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
4743 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
4744
4745 {
4746         struct ll_sb_info *sbi = ll_i2sbi(inode);
4747         struct ptlrpc_request *req;
4748         struct mdt_body *body;
4749         void *lvbdata;
4750         void *lmm;
4751         int lmmsize;
4752         int rc;
4753         ENTRY;
4754
4755         CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
4756                PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
4757                lock->l_lvb_data, lock->l_lvb_len);
4758
4759         if (lock->l_lvb_data != NULL)
4760                 RETURN(0);
4761
4762         /* if layout lock was granted right away, the layout is returned
4763          * within DLM_LVB of dlm reply; otherwise if the lock was ever
4764          * blocked and then granted via completion ast, we have to fetch
4765          * layout here. Please note that we can't use the LVB buffer in
4766          * completion AST because it doesn't have a large enough buffer */
4767         rc = ll_get_default_mdsize(sbi, &lmmsize);
4768         if (rc == 0)
4769                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
4770                                 OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
4771                                 lmmsize, 0, &req);
4772         if (rc < 0)
4773                 RETURN(rc);
4774
4775         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4776         if (body == NULL)
4777                 GOTO(out, rc = -EPROTO);
4778
4779         lmmsize = body->mbo_eadatasize;
4780         if (lmmsize == 0) /* empty layout */
4781                 GOTO(out, rc = 0);
4782
4783         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
4784         if (lmm == NULL)
4785                 GOTO(out, rc = -EFAULT);
4786
4787         OBD_ALLOC_LARGE(lvbdata, lmmsize);
4788         if (lvbdata == NULL)
4789                 GOTO(out, rc = -ENOMEM);
4790
4791         memcpy(lvbdata, lmm, lmmsize);
4792         lock_res_and_lock(lock);
4793         if (unlikely(lock->l_lvb_data == NULL)) {
4794                 lock->l_lvb_type = LVB_T_LAYOUT;
4795                 lock->l_lvb_data = lvbdata;
4796                 lock->l_lvb_len = lmmsize;
4797                 lvbdata = NULL;
4798         }
4799         unlock_res_and_lock(lock);
4800
4801         if (lvbdata)
4802                 OBD_FREE_LARGE(lvbdata, lmmsize);
4803
4804         EXIT;
4805
4806 out:
4807         ptlrpc_req_finished(req);
4808         return rc;
4809 }
4810
4811 /**
4812  * Apply the layout to the inode. Layout lock is held and will be released
4813  * in this function.
4814  */
4815 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
4816                               struct inode *inode)
4817 {
4818         struct ll_inode_info *lli = ll_i2info(inode);
4819         struct ll_sb_info    *sbi = ll_i2sbi(inode);
4820         struct ldlm_lock *lock;
4821         struct cl_object_conf conf;
4822         int rc = 0;
4823         bool lvb_ready;
4824         bool wait_layout = false;
4825         ENTRY;
4826
4827         LASSERT(lustre_handle_is_used(lockh));
4828
4829         lock = ldlm_handle2lock(lockh);
4830         LASSERT(lock != NULL);
4831         LASSERT(ldlm_has_layout(lock));
4832
4833         LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
4834                    PFID(&lli->lli_fid), inode);
4835
4836         /* in case this is a caching lock and reinstate with new inode */
4837         md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
4838
4839         lock_res_and_lock(lock);
4840         lvb_ready = ldlm_is_lvb_ready(lock);
4841         unlock_res_and_lock(lock);
4842
4843         /* checking lvb_ready is racy but this is okay. The worst case is
4844          * that multi processes may configure the file on the same time. */
4845         if (lvb_ready)
4846                 GOTO(out, rc = 0);
4847
4848         rc = ll_layout_fetch(inode, lock);
4849         if (rc < 0)
4850                 GOTO(out, rc);
4851
4852         /* for layout lock, lmm is stored in lock's lvb.
4853          * lvb_data is immutable if the lock is held so it's safe to access it
4854          * without res lock.
4855          *
4856          * set layout to file. Unlikely this will fail as old layout was
4857          * surely eliminated */
4858         memset(&conf, 0, sizeof conf);
4859         conf.coc_opc = OBJECT_CONF_SET;
4860         conf.coc_inode = inode;
4861         conf.coc_lock = lock;
4862         conf.u.coc_layout.lb_buf = lock->l_lvb_data;
4863         conf.u.coc_layout.lb_len = lock->l_lvb_len;
4864         rc = ll_layout_conf(inode, &conf);
4865
4866         /* refresh layout failed, need to wait */
4867         wait_layout = rc == -EBUSY;
4868         EXIT;
4869 out:
4870         LDLM_LOCK_PUT(lock);
4871         ldlm_lock_decref(lockh, mode);
4872
4873         /* wait for IO to complete if it's still being used. */
4874         if (wait_layout) {
4875                 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
4876                        ll_get_fsname(inode->i_sb, NULL, 0),
4877                        PFID(&lli->lli_fid), inode);
4878
4879                 memset(&conf, 0, sizeof conf);
4880                 conf.coc_opc = OBJECT_CONF_WAIT;
4881                 conf.coc_inode = inode;
4882                 rc = ll_layout_conf(inode, &conf);
4883                 if (rc == 0)
4884                         rc = -EAGAIN;
4885
4886                 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
4887                        ll_get_fsname(inode->i_sb, NULL, 0),
4888                        PFID(&lli->lli_fid), rc);
4889         }
4890         RETURN(rc);
4891 }
4892
4893 /**
4894  * Issue layout intent RPC to MDS.
4895  * \param inode [in]    file inode
4896  * \param intent [in]   layout intent
4897  *
4898  * \retval 0    on success
4899  * \retval < 0  error code
4900  */
4901 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
4902 {
4903         struct ll_inode_info  *lli = ll_i2info(inode);
4904         struct ll_sb_info     *sbi = ll_i2sbi(inode);
4905         struct md_op_data     *op_data;
4906         struct lookup_intent it;
4907         struct ptlrpc_request *req;
4908         int rc;
4909         ENTRY;
4910
4911         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
4912                                      0, 0, LUSTRE_OPC_ANY, NULL);
4913         if (IS_ERR(op_data))
4914                 RETURN(PTR_ERR(op_data));
4915
4916         op_data->op_data = intent;
4917         op_data->op_data_size = sizeof(*intent);
4918
4919         memset(&it, 0, sizeof(it));
4920         it.it_op = IT_LAYOUT;
4921         if (intent->li_opc == LAYOUT_INTENT_WRITE ||
4922             intent->li_opc == LAYOUT_INTENT_TRUNC)
4923                 it.it_flags = FMODE_WRITE;
4924
4925         LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
4926                           ll_get_fsname(inode->i_sb, NULL, 0),
4927                           PFID(&lli->lli_fid), inode);
4928
4929         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
4930                             &ll_md_blocking_ast, 0);
4931         if (it.it_request != NULL)
4932                 ptlrpc_req_finished(it.it_request);
4933         it.it_request = NULL;
4934
4935         ll_finish_md_op_data(op_data);
4936
4937         /* set lock data in case this is a new lock */
4938         if (!rc)
4939                 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
4940
4941         ll_intent_drop_lock(&it);
4942
4943         RETURN(rc);
4944 }
4945
4946 /**
4947  * This function checks if there exists a LAYOUT lock on the client side,
4948  * or enqueues it if it doesn't have one in cache.
4949  *
4950  * This function will not hold layout lock so it may be revoked any time after
4951  * this function returns. Any operations depend on layout should be redone
4952  * in that case.
4953  *
4954  * This function should be called before lov_io_init() to get an uptodate
4955  * layout version, the caller should save the version number and after IO
4956  * is finished, this function should be called again to verify that layout
4957  * is not changed during IO time.
4958  */
4959 int ll_layout_refresh(struct inode *inode, __u32 *gen)
4960 {
4961         struct ll_inode_info    *lli = ll_i2info(inode);
4962         struct ll_sb_info       *sbi = ll_i2sbi(inode);
4963         struct lustre_handle lockh;
4964         struct layout_intent intent = {
4965                 .li_opc = LAYOUT_INTENT_ACCESS,
4966         };
4967         enum ldlm_mode mode;
4968         int rc;
4969         ENTRY;
4970
4971         *gen = ll_layout_version_get(lli);
4972         if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
4973                 RETURN(0);
4974
4975         /* sanity checks */
4976         LASSERT(fid_is_sane(ll_inode2fid(inode)));
4977         LASSERT(S_ISREG(inode->i_mode));
4978
4979         /* take layout lock mutex to enqueue layout lock exclusively. */
4980         mutex_lock(&lli->lli_layout_mutex);
4981
4982         while (1) {
4983                 /* mostly layout lock is caching on the local side, so try to
4984                  * match it before grabbing layout lock mutex. */
4985                 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
4986                                        LCK_CR | LCK_CW | LCK_PR | LCK_PW);
4987                 if (mode != 0) { /* hit cached lock */
4988                         rc = ll_layout_lock_set(&lockh, mode, inode);
4989                         if (rc == -EAGAIN)
4990                                 continue;
4991                         break;
4992                 }
4993
4994                 rc = ll_layout_intent(inode, &intent);
4995                 if (rc != 0)
4996                         break;
4997         }
4998
4999         if (rc == 0)
5000                 *gen = ll_layout_version_get(lli);
5001         mutex_unlock(&lli->lli_layout_mutex);
5002
5003         RETURN(rc);
5004 }
5005
5006 /**
5007  * Issue layout intent RPC indicating where in a file an IO is about to write.
5008  *
5009  * \param[in] inode     file inode.
5010  * \param[in] ext       write range with start offset of fille in bytes where
5011  *                      an IO is about to write, and exclusive end offset in
5012  *                      bytes.
5013  *
5014  * \retval 0    on success
5015  * \retval < 0  error code
5016  */
5017 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5018                            struct lu_extent *ext)
5019 {
5020         struct layout_intent intent = {
5021                 .li_opc = opc,
5022                 .li_extent.e_start = ext->e_start,
5023                 .li_extent.e_end = ext->e_end,
5024         };
5025         int rc;
5026         ENTRY;
5027
5028         rc = ll_layout_intent(inode, &intent);
5029
5030         RETURN(rc);
5031 }
5032
5033 /**
5034  *  This function send a restore request to the MDT
5035  */
5036 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5037 {
5038         struct hsm_user_request *hur;
5039         int                      len, rc;
5040         ENTRY;
5041
5042         len = sizeof(struct hsm_user_request) +
5043               sizeof(struct hsm_user_item);
5044         OBD_ALLOC(hur, len);
5045         if (hur == NULL)
5046                 RETURN(-ENOMEM);
5047
5048         hur->hur_request.hr_action = HUA_RESTORE;
5049         hur->hur_request.hr_archive_id = 0;
5050         hur->hur_request.hr_flags = 0;
5051         memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5052                sizeof(hur->hur_user_item[0].hui_fid));
5053         hur->hur_user_item[0].hui_extent.offset = offset;
5054         hur->hur_user_item[0].hui_extent.length = length;
5055         hur->hur_request.hr_itemcount = 1;
5056         rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
5057                            len, hur, NULL);
5058         OBD_FREE(hur, len);
5059         RETURN(rc);
5060 }