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