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