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