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