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