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