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