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