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