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