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