Whamcloud - gitweb
LU-56 lnet: wrong assertion for optimized GET
[fs/lustre-release.git] / lustre / llite / file.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/file.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <lustre_dlm.h>
45 #include <lustre_lite.h>
46 #include <linux/pagemap.h>
47 #include <linux/file.h>
48 #include "llite_internal.h"
49 #include <lustre/ll_fiemap.h>
50
51 #include "cl_object.h"
52
53 struct ll_file_data *ll_file_data_get(void)
54 {
55         struct ll_file_data *fd;
56
57         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
58         return fd;
59 }
60
61 static void ll_file_data_put(struct ll_file_data *fd)
62 {
63         if (fd != NULL)
64                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
65 }
66
67 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
68                           struct lustre_handle *fh)
69 {
70         op_data->op_fid1 = ll_i2info(inode)->lli_fid;
71         op_data->op_attr.ia_mode = inode->i_mode;
72         op_data->op_attr.ia_atime = inode->i_atime;
73         op_data->op_attr.ia_mtime = inode->i_mtime;
74         op_data->op_attr.ia_ctime = inode->i_ctime;
75         op_data->op_attr.ia_size = i_size_read(inode);
76         op_data->op_attr_blocks = inode->i_blocks;
77         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
78                                         ll_inode_to_ext_flags(inode->i_flags);
79         op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
80         if (fh)
81                 op_data->op_handle = *fh;
82         op_data->op_capa1 = ll_mdscapa_get(inode);
83 }
84
85 /**
86  * Closes the IO epoch and packs all the attributes into @op_data for
87  * the CLOSE rpc.
88  */
89 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
90                              struct obd_client_handle *och)
91 {
92         ENTRY;
93
94         op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
95                                  ATTR_MTIME_SET | ATTR_CTIME_SET;
96
97         if (!(och->och_flags & FMODE_WRITE))
98                 goto out;
99
100         if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
101                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
102         else
103                 ll_ioepoch_close(inode, op_data, &och, 0);
104
105 out:
106         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
107         ll_prep_md_op_data(op_data, inode, NULL, NULL,
108                            0, 0, LUSTRE_OPC_ANY, NULL);
109         EXIT;
110 }
111
112 static int ll_close_inode_openhandle(struct obd_export *md_exp,
113                                      struct inode *inode,
114                                      struct obd_client_handle *och)
115 {
116         struct obd_export *exp = ll_i2mdexp(inode);
117         struct md_op_data *op_data;
118         struct ptlrpc_request *req = NULL;
119         struct obd_device *obd = class_exp2obd(exp);
120         int epoch_close = 1;
121         int rc;
122         ENTRY;
123
124         if (obd == NULL) {
125                 /*
126                  * XXX: in case of LMV, is this correct to access
127                  * ->exp_handle?
128                  */
129                 CERROR("Invalid MDC connection handle "LPX64"\n",
130                        ll_i2mdexp(inode)->exp_handle.h_cookie);
131                 GOTO(out, rc = 0);
132         }
133
134         OBD_ALLOC_PTR(op_data);
135         if (op_data == NULL)
136                 GOTO(out, rc = -ENOMEM); // XXX We leak openhandle and request here.
137
138         ll_prepare_close(inode, op_data, och);
139         epoch_close = (op_data->op_flags & MF_EPOCH_CLOSE);
140         rc = md_close(md_exp, op_data, och->och_mod, &req);
141         if (rc == -EAGAIN) {
142                 /* This close must have the epoch closed. */
143                 LASSERT(epoch_close);
144                 /* MDS has instructed us to obtain Size-on-MDS attribute from
145                  * OSTs and send setattr to back to MDS. */
146                 rc = ll_som_update(inode, op_data);
147                 if (rc) {
148                         CERROR("inode %lu mdc Size-on-MDS update failed: "
149                                "rc = %d\n", inode->i_ino, rc);
150                         rc = 0;
151                 }
152         } else if (rc) {
153                 CERROR("inode %lu mdc close failed: rc = %d\n",
154                        inode->i_ino, rc);
155         }
156         ll_finish_md_op_data(op_data);
157
158         if (rc == 0) {
159                 rc = ll_objects_destroy(req, inode);
160                 if (rc)
161                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
162                                inode->i_ino, rc);
163         }
164
165         EXIT;
166 out:
167
168         if (exp_connect_som(exp) && !epoch_close &&
169             S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
170                 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
171         } else {
172                 md_clear_open_replay_data(md_exp, och);
173                 /* Free @och if it is not waiting for DONE_WRITING. */
174                 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
175                 OBD_FREE_PTR(och);
176         }
177         if (req) /* This is close request */
178                 ptlrpc_req_finished(req);
179         return rc;
180 }
181
182 int ll_md_real_close(struct inode *inode, int flags)
183 {
184         struct ll_inode_info *lli = ll_i2info(inode);
185         struct obd_client_handle **och_p;
186         struct obd_client_handle *och;
187         __u64 *och_usecount;
188         int rc = 0;
189         ENTRY;
190
191         if (flags & FMODE_WRITE) {
192                 och_p = &lli->lli_mds_write_och;
193                 och_usecount = &lli->lli_open_fd_write_count;
194         } else if (flags & FMODE_EXEC) {
195                 och_p = &lli->lli_mds_exec_och;
196                 och_usecount = &lli->lli_open_fd_exec_count;
197         } else {
198                 LASSERT(flags & FMODE_READ);
199                 och_p = &lli->lli_mds_read_och;
200                 och_usecount = &lli->lli_open_fd_read_count;
201         }
202
203         cfs_mutex_lock(&lli->lli_och_mutex);
204         if (*och_usecount) { /* There are still users of this handle, so
205                                 skip freeing it. */
206                 cfs_mutex_unlock(&lli->lli_och_mutex);
207                 RETURN(0);
208         }
209         och=*och_p;
210         *och_p = NULL;
211         cfs_mutex_unlock(&lli->lli_och_mutex);
212
213         if (och) { /* There might be a race and somebody have freed this och
214                       already */
215                 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
216                                                inode, och);
217         }
218
219         RETURN(rc);
220 }
221
222 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
223                 struct file *file)
224 {
225         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
226         struct ll_inode_info *lli = ll_i2info(inode);
227         int rc = 0;
228         ENTRY;
229
230         /* clear group lock, if present */
231         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
232                 ll_put_grouplock(inode, file, fd->fd_grouplock.cg_gid);
233
234         /* Let's see if we have good enough OPEN lock on the file and if
235            we can skip talking to MDS */
236         if (file->f_dentry->d_inode) { /* Can this ever be false? */
237                 int lockmode;
238                 int flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
239                 struct lustre_handle lockh;
240                 struct inode *inode = file->f_dentry->d_inode;
241                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
242
243                 cfs_mutex_lock(&lli->lli_och_mutex);
244                 if (fd->fd_omode & FMODE_WRITE) {
245                         lockmode = LCK_CW;
246                         LASSERT(lli->lli_open_fd_write_count);
247                         lli->lli_open_fd_write_count--;
248                 } else if (fd->fd_omode & FMODE_EXEC) {
249                         lockmode = LCK_PR;
250                         LASSERT(lli->lli_open_fd_exec_count);
251                         lli->lli_open_fd_exec_count--;
252                 } else {
253                         lockmode = LCK_CR;
254                         LASSERT(lli->lli_open_fd_read_count);
255                         lli->lli_open_fd_read_count--;
256                 }
257                 cfs_mutex_unlock(&lli->lli_och_mutex);
258
259                 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
260                                    LDLM_IBITS, &policy, lockmode,
261                                    &lockh)) {
262                         rc = ll_md_real_close(file->f_dentry->d_inode,
263                                               fd->fd_omode);
264                 }
265         } else {
266                 CERROR("Releasing a file %p with negative dentry %p. Name %s",
267                        file, file->f_dentry, file->f_dentry->d_name.name);
268         }
269
270         LUSTRE_FPRIVATE(file) = NULL;
271         ll_file_data_put(fd);
272         ll_capa_close(inode);
273
274         RETURN(rc);
275 }
276
277 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm);
278
279 /* While this returns an error code, fput() the caller does not, so we need
280  * to make every effort to clean up all of our state here.  Also, applications
281  * rarely check close errors and even if an error is returned they will not
282  * re-try the close call.
283  */
284 int ll_file_release(struct inode *inode, struct file *file)
285 {
286         struct ll_file_data *fd;
287         struct ll_sb_info *sbi = ll_i2sbi(inode);
288         struct ll_inode_info *lli = ll_i2info(inode);
289         struct lov_stripe_md *lsm = lli->lli_smd;
290         int rc;
291         ENTRY;
292
293         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
294                inode->i_generation, inode);
295
296 #ifdef CONFIG_FS_POSIX_ACL
297         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
298             inode == inode->i_sb->s_root->d_inode) {
299                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
300
301                 LASSERT(fd != NULL);
302                 if (unlikely(fd->fd_flags & LL_FILE_RMTACL)) {
303                         fd->fd_flags &= ~LL_FILE_RMTACL;
304                         rct_del(&sbi->ll_rct, cfs_curproc_pid());
305                         et_search_free(&sbi->ll_et, cfs_curproc_pid());
306                 }
307         }
308 #endif
309
310         if (inode->i_sb->s_root != file->f_dentry)
311                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
312         fd = LUSTRE_FPRIVATE(file);
313         LASSERT(fd != NULL);
314
315         /* The last ref on @file, maybe not the the owner pid of statahead.
316          * Different processes can open the same dir, "ll_opendir_key" means:
317          * it is me that should stop the statahead thread. */
318         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd &&
319             lli->lli_opendir_pid != 0)
320                 ll_stop_statahead(inode, lli->lli_opendir_key);
321
322         if (inode->i_sb->s_root == file->f_dentry) {
323                 LUSTRE_FPRIVATE(file) = NULL;
324                 ll_file_data_put(fd);
325                 RETURN(0);
326         }
327
328         if (!S_ISDIR(inode->i_mode)) {
329                 if (lsm)
330                         lov_test_and_clear_async_rc(lsm);
331                 lli->lli_async_rc = 0;
332         }
333
334         rc = ll_md_close(sbi->ll_md_exp, inode, file);
335
336         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
337                 libcfs_debug_dumplog();
338
339         RETURN(rc);
340 }
341
342 static int ll_intent_file_open(struct file *file, void *lmm,
343                                int lmmsize, struct lookup_intent *itp)
344 {
345         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
346         struct dentry *parent = file->f_dentry->d_parent;
347         const char *name = file->f_dentry->d_name.name;
348         const int len = file->f_dentry->d_name.len;
349         struct md_op_data *op_data;
350         struct ptlrpc_request *req;
351         __u32 opc = LUSTRE_OPC_ANY;
352         int rc;
353         ENTRY;
354
355         if (!parent)
356                 RETURN(-ENOENT);
357
358         /* Usually we come here only for NFSD, and we want open lock.
359            But we can also get here with pre 2.6.15 patchless kernels, and in
360            that case that lock is also ok */
361         /* We can also get here if there was cached open handle in revalidate_it
362          * but it disappeared while we were getting from there to ll_file_open.
363          * But this means this file was closed and immediatelly opened which
364          * makes a good candidate for using OPEN lock */
365         /* If lmmsize & lmm are not 0, we are just setting stripe info
366          * parameters. No need for the open lock */
367         if (lmm == NULL && lmmsize == 0) {
368                 itp->it_flags |= MDS_OPEN_LOCK;
369                 if (itp->it_flags & FMODE_WRITE)
370                         opc = LUSTRE_OPC_CREATE;
371         }
372
373         op_data  = ll_prep_md_op_data(NULL, parent->d_inode,
374                                       file->f_dentry->d_inode, name, len,
375                                       O_RDWR, opc, NULL);
376         if (IS_ERR(op_data))
377                 RETURN(PTR_ERR(op_data));
378
379         rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
380                             0 /*unused */, &req, ll_md_blocking_ast, 0);
381         ll_finish_md_op_data(op_data);
382         if (rc == -ESTALE) {
383                 /* reason for keep own exit path - don`t flood log
384                 * with messages with -ESTALE errors.
385                 */
386                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
387                      it_open_error(DISP_OPEN_OPEN, itp))
388                         GOTO(out, rc);
389                 ll_release_openhandle(file->f_dentry, itp);
390                 GOTO(out, rc);
391         }
392
393         if (it_disposition(itp, DISP_LOOKUP_NEG))
394                 GOTO(out, rc = -ENOENT);
395
396         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
397                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
398                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
399                 GOTO(out, rc);
400         }
401
402         rc = ll_prep_inode(&file->f_dentry->d_inode, req, NULL);
403         if (!rc && itp->d.lustre.it_lock_mode)
404                 ll_set_lock_data(sbi->ll_md_exp, file->f_dentry->d_inode,
405                                  itp, NULL);
406
407 out:
408         ptlrpc_req_finished(itp->d.lustre.it_data);
409         it_clear_disposition(itp, DISP_ENQ_COMPLETE);
410         ll_intent_drop_lock(itp);
411
412         RETURN(rc);
413 }
414
415 /**
416  * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
417  * not believe attributes if a few ioepoch holders exist. Attributes for
418  * previous ioepoch if new one is opened are also skipped by MDS.
419  */
420 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
421 {
422         if (ioepoch && lli->lli_ioepoch != ioepoch) {
423                 lli->lli_ioepoch = ioepoch;
424                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
425                        ioepoch, PFID(&lli->lli_fid));
426         }
427 }
428
429 static int ll_och_fill(struct obd_export *md_exp, struct ll_inode_info *lli,
430                        struct lookup_intent *it, struct obd_client_handle *och)
431 {
432         struct ptlrpc_request *req = it->d.lustre.it_data;
433         struct mdt_body *body;
434
435         LASSERT(och);
436
437         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
438         LASSERT(body != NULL);                      /* reply already checked out */
439
440         memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
441         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
442         och->och_fid = lli->lli_fid;
443         och->och_flags = it->it_flags;
444         ll_ioepoch_open(lli, body->ioepoch);
445
446         return md_set_open_replay_data(md_exp, och, req);
447 }
448
449 int ll_local_open(struct file *file, struct lookup_intent *it,
450                   struct ll_file_data *fd, struct obd_client_handle *och)
451 {
452         struct inode *inode = file->f_dentry->d_inode;
453         struct ll_inode_info *lli = ll_i2info(inode);
454         ENTRY;
455
456         LASSERT(!LUSTRE_FPRIVATE(file));
457
458         LASSERT(fd != NULL);
459
460         if (och) {
461                 struct ptlrpc_request *req = it->d.lustre.it_data;
462                 struct mdt_body *body;
463                 int rc;
464
465                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, lli, it, och);
466                 if (rc)
467                         RETURN(rc);
468
469                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
470                 if ((it->it_flags & FMODE_WRITE) &&
471                     (body->valid & OBD_MD_FLSIZE))
472                         CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
473                                lli->lli_ioepoch, PFID(&lli->lli_fid));
474         }
475
476         LUSTRE_FPRIVATE(file) = fd;
477         ll_readahead_init(inode, &fd->fd_ras);
478         fd->fd_omode = it->it_flags;
479         RETURN(0);
480 }
481
482 /* Open a file, and (for the very first open) create objects on the OSTs at
483  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
484  * creation or open until ll_lov_setstripe() ioctl is called.
485  *
486  * If we already have the stripe MD locally then we don't request it in
487  * md_open(), by passing a lmm_size = 0.
488  *
489  * It is up to the application to ensure no other processes open this file
490  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
491  * used.  We might be able to avoid races of that sort by getting lli_open_sem
492  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
493  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
494  */
495 int ll_file_open(struct inode *inode, struct file *file)
496 {
497         struct ll_inode_info *lli = ll_i2info(inode);
498         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
499                                           .it_flags = file->f_flags };
500         struct lov_stripe_md *lsm;
501         struct obd_client_handle **och_p = NULL;
502         __u64 *och_usecount = NULL;
503         struct ll_file_data *fd;
504         int rc = 0, opendir_set = 0;
505         ENTRY;
506
507         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
508                inode->i_generation, inode, file->f_flags);
509
510         it = file->private_data; /* XXX: compat macro */
511         file->private_data = NULL; /* prevent ll_local_open assertion */
512
513         fd = ll_file_data_get();
514         if (fd == NULL)
515                 GOTO(out_och_free, rc = -ENOMEM);
516
517         fd->fd_file = file;
518         if (S_ISDIR(inode->i_mode)) {
519                 cfs_spin_lock(&lli->lli_sa_lock);
520                 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL &&
521                     lli->lli_opendir_pid == 0) {
522                         lli->lli_opendir_key = fd;
523                         lli->lli_opendir_pid = cfs_curproc_pid();
524                         opendir_set = 1;
525                 }
526                 cfs_spin_unlock(&lli->lli_sa_lock);
527         }
528
529         if (inode->i_sb->s_root == file->f_dentry) {
530                 LUSTRE_FPRIVATE(file) = fd;
531                 RETURN(0);
532         }
533
534         if (!it || !it->d.lustre.it_disposition) {
535                 /* Convert f_flags into access mode. We cannot use file->f_mode,
536                  * because everything but O_ACCMODE mask was stripped from
537                  * there */
538                 if ((oit.it_flags + 1) & O_ACCMODE)
539                         oit.it_flags++;
540                 if (file->f_flags & O_TRUNC)
541                         oit.it_flags |= FMODE_WRITE;
542
543                 /* kernel only call f_op->open in dentry_open.  filp_open calls
544                  * dentry_open after call to open_namei that checks permissions.
545                  * Only nfsd_open call dentry_open directly without checking
546                  * permissions and because of that this code below is safe. */
547                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
548                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
549
550                 /* We do not want O_EXCL here, presumably we opened the file
551                  * already? XXX - NFS implications? */
552                 oit.it_flags &= ~O_EXCL;
553
554                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
555                  * created if necessary, then "IT_CREAT" should be set to keep
556                  * consistent with it */
557                 if (oit.it_flags & O_CREAT)
558                         oit.it_op |= IT_CREAT;
559
560                 it = &oit;
561         }
562
563 restart:
564         /* Let's see if we have file open on MDS already. */
565         if (it->it_flags & FMODE_WRITE) {
566                 och_p = &lli->lli_mds_write_och;
567                 och_usecount = &lli->lli_open_fd_write_count;
568         } else if (it->it_flags & FMODE_EXEC) {
569                 och_p = &lli->lli_mds_exec_och;
570                 och_usecount = &lli->lli_open_fd_exec_count;
571          } else {
572                 och_p = &lli->lli_mds_read_och;
573                 och_usecount = &lli->lli_open_fd_read_count;
574         }
575
576         cfs_mutex_lock(&lli->lli_och_mutex);
577         if (*och_p) { /* Open handle is present */
578                 if (it_disposition(it, DISP_OPEN_OPEN)) {
579                         /* Well, there's extra open request that we do not need,
580                            let's close it somehow. This will decref request. */
581                         rc = it_open_error(DISP_OPEN_OPEN, it);
582                         if (rc) {
583                                 cfs_mutex_unlock(&lli->lli_och_mutex);
584                                 GOTO(out_openerr, rc);
585                         }
586
587                         ll_release_openhandle(file->f_dentry, it);
588                 }
589                 (*och_usecount)++;
590
591                 rc = ll_local_open(file, it, fd, NULL);
592                 if (rc) {
593                         (*och_usecount)--;
594                         cfs_mutex_unlock(&lli->lli_och_mutex);
595                         GOTO(out_openerr, rc);
596                 }
597         } else {
598                 LASSERT(*och_usecount == 0);
599                 if (!it->d.lustre.it_disposition) {
600                         /* We cannot just request lock handle now, new ELC code
601                            means that one of other OPEN locks for this file
602                            could be cancelled, and since blocking ast handler
603                            would attempt to grab och_mutex as well, that would
604                            result in a deadlock */
605                         cfs_mutex_unlock(&lli->lli_och_mutex);
606                         it->it_create_mode |= M_CHECK_STALE;
607                         rc = ll_intent_file_open(file, NULL, 0, it);
608                         it->it_create_mode &= ~M_CHECK_STALE;
609                         if (rc)
610                                 GOTO(out_openerr, rc);
611
612                         goto restart;
613                 }
614                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
615                 if (!*och_p)
616                         GOTO(out_och_free, rc = -ENOMEM);
617
618                 (*och_usecount)++;
619
620                 /* md_intent_lock() didn't get a request ref if there was an
621                  * open error, so don't do cleanup on the request here
622                  * (bug 3430) */
623                 /* XXX (green): Should not we bail out on any error here, not
624                  * just open error? */
625                 rc = it_open_error(DISP_OPEN_OPEN, it);
626                 if (rc)
627                         GOTO(out_och_free, rc);
628
629                 LASSERT(it_disposition(it, DISP_ENQ_OPEN_REF));
630
631                 rc = ll_local_open(file, it, fd, *och_p);
632                 if (rc)
633                         GOTO(out_och_free, rc);
634         }
635         cfs_mutex_unlock(&lli->lli_och_mutex);
636         fd = NULL;
637
638         /* Must do this outside lli_och_mutex lock to prevent deadlock where
639            different kind of OPEN lock for this same inode gets cancelled
640            by ldlm_cancel_lru */
641         if (!S_ISREG(inode->i_mode))
642                 GOTO(out_och_free, rc);
643
644         ll_capa_open(inode);
645
646         lsm = lli->lli_smd;
647         if (lsm == NULL) {
648                 if (file->f_flags & O_LOV_DELAY_CREATE ||
649                     !(file->f_mode & FMODE_WRITE)) {
650                         CDEBUG(D_INODE, "object creation was delayed\n");
651                         GOTO(out_och_free, rc);
652                 }
653         }
654         file->f_flags &= ~O_LOV_DELAY_CREATE;
655         GOTO(out_och_free, rc);
656
657 out_och_free:
658         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
659                 ptlrpc_req_finished(it->d.lustre.it_data);
660                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
661         }
662
663         if (rc) {
664                 if (och_p && *och_p) {
665                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
666                         *och_p = NULL; /* OBD_FREE writes some magic there */
667                         (*och_usecount)--;
668                 }
669                 cfs_mutex_unlock(&lli->lli_och_mutex);
670
671 out_openerr:
672                 if (opendir_set != 0)
673                         ll_stop_statahead(inode, lli->lli_opendir_key);
674                 if (fd != NULL)
675                         ll_file_data_put(fd);
676         } else {
677                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
678         }
679
680         return rc;
681 }
682
683 /* Fills the obdo with the attributes for the lsm */
684 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
685                           struct obd_capa *capa, struct obdo *obdo,
686                           __u64 ioepoch, int sync)
687 {
688         struct ptlrpc_request_set *set;
689         struct obd_info            oinfo = { { { 0 } } };
690         int                        rc;
691
692         ENTRY;
693
694         LASSERT(lsm != NULL);
695
696         oinfo.oi_md = lsm;
697         oinfo.oi_oa = obdo;
698         oinfo.oi_oa->o_id = lsm->lsm_object_id;
699         oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
700         oinfo.oi_oa->o_mode = S_IFREG;
701         oinfo.oi_oa->o_ioepoch = ioepoch;
702         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
703                                OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
704                                OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
705                                OBD_MD_FLMTIME | OBD_MD_FLCTIME |
706                                OBD_MD_FLGROUP | OBD_MD_FLEPOCH |
707                                OBD_MD_FLDATAVERSION;
708         oinfo.oi_capa = capa;
709         if (sync) {
710                 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
711                 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
712         }
713
714         set = ptlrpc_prep_set();
715         if (set == NULL) {
716                 CERROR("can't allocate ptlrpc set\n");
717                 rc = -ENOMEM;
718         } else {
719                 rc = obd_getattr_async(exp, &oinfo, set);
720                 if (rc == 0)
721                         rc = ptlrpc_set_wait(set);
722                 ptlrpc_set_destroy(set);
723         }
724         if (rc == 0)
725                 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
726                                          OBD_MD_FLATIME | OBD_MD_FLMTIME |
727                                          OBD_MD_FLCTIME | OBD_MD_FLSIZE |
728                                          OBD_MD_FLDATAVERSION);
729         RETURN(rc);
730 }
731
732 /**
733   * Performs the getattr on the inode and updates its fields.
734   * If @sync != 0, perform the getattr under the server-side lock.
735   */
736 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
737                      __u64 ioepoch, int sync)
738 {
739         struct ll_inode_info *lli  = ll_i2info(inode);
740         struct obd_capa      *capa = ll_mdscapa_get(inode);
741         int rc;
742         ENTRY;
743
744         rc = ll_lsm_getattr(lli->lli_smd, ll_i2dtexp(inode),
745                             capa, obdo, ioepoch, sync);
746         capa_put(capa);
747         if (rc == 0) {
748                 obdo_refresh_inode(inode, obdo, obdo->o_valid);
749                 CDEBUG(D_INODE,
750                        "objid "LPX64" size %llu, blocks %llu, blksize %lu\n",
751                        lli->lli_smd->lsm_object_id, i_size_read(inode),
752                        (unsigned long long)inode->i_blocks,
753                        (unsigned long)ll_inode_blksize(inode));
754         }
755         RETURN(rc);
756 }
757
758 int ll_merge_lvb(struct inode *inode)
759 {
760         struct ll_inode_info *lli = ll_i2info(inode);
761         struct ll_sb_info *sbi = ll_i2sbi(inode);
762         struct ost_lvb lvb;
763         int rc;
764
765         ENTRY;
766
767         ll_inode_size_lock(inode, 1);
768         inode_init_lvb(inode, &lvb);
769
770         /* merge timestamps the most resently obtained from mds with
771            timestamps obtained from osts */
772         lvb.lvb_atime = lli->lli_lvb.lvb_atime;
773         lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
774         lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
775         rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
776         cl_isize_write_nolock(inode, lvb.lvb_size);
777
778         CDEBUG(D_VFSTRACE, DFID" updating i_size "LPU64"\n",
779                PFID(&lli->lli_fid), lvb.lvb_size);
780         inode->i_blocks = lvb.lvb_blocks;
781
782         LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
783         LTIME_S(inode->i_atime) = lvb.lvb_atime;
784         LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
785         ll_inode_size_unlock(inode, 1);
786
787         RETURN(rc);
788 }
789
790 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
791                      lstat_t *st)
792 {
793         struct obdo obdo = { 0 };
794         int rc;
795
796         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
797         if (rc == 0) {
798                 st->st_size   = obdo.o_size;
799                 st->st_blocks = obdo.o_blocks;
800                 st->st_mtime  = obdo.o_mtime;
801                 st->st_atime  = obdo.o_atime;
802                 st->st_ctime  = obdo.o_ctime;
803         }
804         return rc;
805 }
806
807 void ll_io_init(struct cl_io *io, const struct file *file, int write)
808 {
809         struct inode *inode = file->f_dentry->d_inode;
810
811         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
812         if (write) {
813                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
814                 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC || IS_SYNC(inode);
815         }
816         io->ci_obj     = ll_i2info(inode)->lli_clob;
817         io->ci_lockreq = CILR_MAYBE;
818         if (ll_file_nolock(file)) {
819                 io->ci_lockreq = CILR_NEVER;
820                 io->ci_no_srvlock = 1;
821         } else if (file->f_flags & O_APPEND) {
822                 io->ci_lockreq = CILR_MANDATORY;
823         }
824 }
825
826 static ssize_t ll_file_io_generic(const struct lu_env *env,
827                 struct vvp_io_args *args, struct file *file,
828                 enum cl_io_type iot, loff_t *ppos, size_t count)
829 {
830         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
831         struct cl_io         *io;
832         ssize_t               result;
833         ENTRY;
834
835         io = ccc_env_thread_io(env);
836         ll_io_init(io, file, iot == CIT_WRITE);
837
838         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
839                 struct vvp_io *vio = vvp_env_io(env);
840                 struct ccc_io *cio = ccc_env_io(env);
841                 int write_mutex_locked = 0;
842
843                 cio->cui_fd  = LUSTRE_FPRIVATE(file);
844                 vio->cui_io_subtype = args->via_io_subtype;
845
846                 switch (vio->cui_io_subtype) {
847                 case IO_NORMAL:
848                         cio->cui_iov = args->u.normal.via_iov;
849                         cio->cui_nrsegs = args->u.normal.via_nrsegs;
850                         cio->cui_tot_nrsegs = cio->cui_nrsegs;
851 #ifndef HAVE_FILE_WRITEV
852                         cio->cui_iocb = args->u.normal.via_iocb;
853 #endif
854                         if ((iot == CIT_WRITE) &&
855                             !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
856                                 if (cfs_mutex_lock_interruptible(&lli->
857                                                                lli_write_mutex))
858                                         GOTO(out, result = -ERESTARTSYS);
859                                 write_mutex_locked = 1;
860                         } else if (iot == CIT_READ) {
861                                 cfs_down_read(&lli->lli_trunc_sem);
862                         }
863                         break;
864                 case IO_SENDFILE:
865                         vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
866                         vio->u.sendfile.cui_target = args->u.sendfile.via_target;
867                         break;
868                 case IO_SPLICE:
869                         vio->u.splice.cui_pipe = args->u.splice.via_pipe;
870                         vio->u.splice.cui_flags = args->u.splice.via_flags;
871                         break;
872                 default:
873                         CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
874                         LBUG();
875                 }
876                 result = cl_io_loop(env, io);
877                 if (write_mutex_locked)
878                         cfs_mutex_unlock(&lli->lli_write_mutex);
879                 else if (args->via_io_subtype == IO_NORMAL && iot == CIT_READ)
880                         cfs_up_read(&lli->lli_trunc_sem);
881         } else {
882                 /* cl_io_rw_init() handled IO */
883                 result = io->ci_result;
884         }
885
886         if (io->ci_nob > 0) {
887                 result = io->ci_nob;
888                 *ppos = io->u.ci_wr.wr.crw_pos;
889         }
890         GOTO(out, result);
891 out:
892         cl_io_fini(env, io);
893
894         if (iot == CIT_READ) {
895                 if (result >= 0)
896                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
897                                            LPROC_LL_READ_BYTES, result);
898         } else if (iot == CIT_WRITE) {
899                 if (result >= 0) {
900                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
901                                            LPROC_LL_WRITE_BYTES, result);
902                         lli->lli_write_rc = 0;
903                 } else {
904                         lli->lli_write_rc = result;
905                 }
906         }
907
908         return result;
909 }
910
911
912 /*
913  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
914  */
915 static int ll_file_get_iov_count(const struct iovec *iov,
916                                  unsigned long *nr_segs, size_t *count)
917 {
918         size_t cnt = 0;
919         unsigned long seg;
920
921         for (seg = 0; seg < *nr_segs; seg++) {
922                 const struct iovec *iv = &iov[seg];
923
924                 /*
925                  * If any segment has a negative length, or the cumulative
926                  * length ever wraps negative then return -EINVAL.
927                  */
928                 cnt += iv->iov_len;
929                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
930                         return -EINVAL;
931                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
932                         continue;
933                 if (seg == 0)
934                         return -EFAULT;
935                 *nr_segs = seg;
936                 cnt -= iv->iov_len;   /* This segment is no good */
937                 break;
938         }
939         *count = cnt;
940         return 0;
941 }
942
943 #ifdef HAVE_FILE_READV
944 static ssize_t ll_file_readv(struct file *file, const struct iovec *iov,
945                               unsigned long nr_segs, loff_t *ppos)
946 {
947         struct lu_env      *env;
948         struct vvp_io_args *args;
949         size_t              count;
950         ssize_t             result;
951         int                 refcheck;
952         ENTRY;
953
954         result = ll_file_get_iov_count(iov, &nr_segs, &count);
955         if (result)
956                 RETURN(result);
957
958         env = cl_env_get(&refcheck);
959         if (IS_ERR(env))
960                 RETURN(PTR_ERR(env));
961
962         args = vvp_env_args(env, IO_NORMAL);
963         args->u.normal.via_iov = (struct iovec *)iov;
964         args->u.normal.via_nrsegs = nr_segs;
965
966         result = ll_file_io_generic(env, args, file, CIT_READ, ppos, count);
967         cl_env_put(env, &refcheck);
968         RETURN(result);
969 }
970
971 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
972                             loff_t *ppos)
973 {
974         struct lu_env *env;
975         struct iovec  *local_iov;
976         ssize_t        result;
977         int            refcheck;
978         ENTRY;
979
980         env = cl_env_get(&refcheck);
981         if (IS_ERR(env))
982                 RETURN(PTR_ERR(env));
983
984         local_iov = &vvp_env_info(env)->vti_local_iov;
985         local_iov->iov_base = (void __user *)buf;
986         local_iov->iov_len = count;
987         result = ll_file_readv(file, local_iov, 1, ppos);
988         cl_env_put(env, &refcheck);
989         RETURN(result);
990 }
991
992 #else
993 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
994                                 unsigned long nr_segs, loff_t pos)
995 {
996         struct lu_env      *env;
997         struct vvp_io_args *args;
998         size_t              count;
999         ssize_t             result;
1000         int                 refcheck;
1001         ENTRY;
1002
1003         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1004         if (result)
1005                 RETURN(result);
1006
1007         env = cl_env_get(&refcheck);
1008         if (IS_ERR(env))
1009                 RETURN(PTR_ERR(env));
1010
1011         args = vvp_env_args(env, IO_NORMAL);
1012         args->u.normal.via_iov = (struct iovec *)iov;
1013         args->u.normal.via_nrsegs = nr_segs;
1014         args->u.normal.via_iocb = iocb;
1015
1016         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1017                                     &iocb->ki_pos, count);
1018         cl_env_put(env, &refcheck);
1019         RETURN(result);
1020 }
1021
1022 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1023                             loff_t *ppos)
1024 {
1025         struct lu_env *env;
1026         struct iovec  *local_iov;
1027         struct kiocb  *kiocb;
1028         ssize_t        result;
1029         int            refcheck;
1030         ENTRY;
1031
1032         env = cl_env_get(&refcheck);
1033         if (IS_ERR(env))
1034                 RETURN(PTR_ERR(env));
1035
1036         local_iov = &vvp_env_info(env)->vti_local_iov;
1037         kiocb = &vvp_env_info(env)->vti_kiocb;
1038         local_iov->iov_base = (void __user *)buf;
1039         local_iov->iov_len = count;
1040         init_sync_kiocb(kiocb, file);
1041         kiocb->ki_pos = *ppos;
1042         kiocb->ki_left = count;
1043
1044         result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1045         *ppos = kiocb->ki_pos;
1046
1047         cl_env_put(env, &refcheck);
1048         RETURN(result);
1049 }
1050 #endif
1051
1052 /*
1053  * Write to a file (through the page cache).
1054  */
1055 #ifdef HAVE_FILE_WRITEV
1056 static ssize_t ll_file_writev(struct file *file, const struct iovec *iov,
1057                               unsigned long nr_segs, loff_t *ppos)
1058 {
1059         struct lu_env      *env;
1060         struct vvp_io_args *args;
1061         size_t              count;
1062         ssize_t             result;
1063         int                 refcheck;
1064         ENTRY;
1065
1066         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1067         if (result)
1068                 RETURN(result);
1069
1070         env = cl_env_get(&refcheck);
1071         if (IS_ERR(env))
1072                 RETURN(PTR_ERR(env));
1073
1074         args = vvp_env_args(env, IO_NORMAL);
1075         args->u.normal.via_iov = (struct iovec *)iov;
1076         args->u.normal.via_nrsegs = nr_segs;
1077
1078         result = ll_file_io_generic(env, args, file, CIT_WRITE, ppos, count);
1079         cl_env_put(env, &refcheck);
1080         RETURN(result);
1081 }
1082
1083 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1084                              loff_t *ppos)
1085 {
1086         struct lu_env    *env;
1087         struct iovec     *local_iov;
1088         ssize_t           result;
1089         int               refcheck;
1090         ENTRY;
1091
1092         env = cl_env_get(&refcheck);
1093         if (IS_ERR(env))
1094                 RETURN(PTR_ERR(env));
1095
1096         local_iov = &vvp_env_info(env)->vti_local_iov;
1097         local_iov->iov_base = (void __user *)buf;
1098         local_iov->iov_len = count;
1099
1100         result = ll_file_writev(file, local_iov, 1, ppos);
1101         cl_env_put(env, &refcheck);
1102         RETURN(result);
1103 }
1104
1105 #else /* AIO stuff */
1106 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1107                                  unsigned long nr_segs, loff_t pos)
1108 {
1109         struct lu_env      *env;
1110         struct vvp_io_args *args;
1111         size_t              count;
1112         ssize_t             result;
1113         int                 refcheck;
1114         ENTRY;
1115
1116         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1117         if (result)
1118                 RETURN(result);
1119
1120         env = cl_env_get(&refcheck);
1121         if (IS_ERR(env))
1122                 RETURN(PTR_ERR(env));
1123
1124         args = vvp_env_args(env, IO_NORMAL);
1125         args->u.normal.via_iov = (struct iovec *)iov;
1126         args->u.normal.via_nrsegs = nr_segs;
1127         args->u.normal.via_iocb = iocb;
1128
1129         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1130                                   &iocb->ki_pos, count);
1131         cl_env_put(env, &refcheck);
1132         RETURN(result);
1133 }
1134
1135 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1136                              loff_t *ppos)
1137 {
1138         struct lu_env *env;
1139         struct iovec  *local_iov;
1140         struct kiocb  *kiocb;
1141         ssize_t        result;
1142         int            refcheck;
1143         ENTRY;
1144
1145         env = cl_env_get(&refcheck);
1146         if (IS_ERR(env))
1147                 RETURN(PTR_ERR(env));
1148
1149         local_iov = &vvp_env_info(env)->vti_local_iov;
1150         kiocb = &vvp_env_info(env)->vti_kiocb;
1151         local_iov->iov_base = (void __user *)buf;
1152         local_iov->iov_len = count;
1153         init_sync_kiocb(kiocb, file);
1154         kiocb->ki_pos = *ppos;
1155         kiocb->ki_left = count;
1156
1157         result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1158         *ppos = kiocb->ki_pos;
1159
1160         cl_env_put(env, &refcheck);
1161         RETURN(result);
1162 }
1163 #endif
1164
1165
1166 #ifdef HAVE_KERNEL_SENDFILE
1167 /*
1168  * Send file content (through pagecache) somewhere with helper
1169  */
1170 static ssize_t ll_file_sendfile(struct file *in_file, loff_t *ppos,size_t count,
1171                                 read_actor_t actor, void *target)
1172 {
1173         struct lu_env      *env;
1174         struct vvp_io_args *args;
1175         ssize_t             result;
1176         int                 refcheck;
1177         ENTRY;
1178
1179         env = cl_env_get(&refcheck);
1180         if (IS_ERR(env))
1181                 RETURN(PTR_ERR(env));
1182
1183         args = vvp_env_args(env, IO_SENDFILE);
1184         args->u.sendfile.via_target = target;
1185         args->u.sendfile.via_actor = actor;
1186
1187         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1188         cl_env_put(env, &refcheck);
1189         RETURN(result);
1190 }
1191 #endif
1192
1193 #ifdef HAVE_KERNEL_SPLICE_READ
1194 /*
1195  * Send file content (through pagecache) somewhere with helper
1196  */
1197 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1198                                    struct pipe_inode_info *pipe, size_t count,
1199                                    unsigned int flags)
1200 {
1201         struct lu_env      *env;
1202         struct vvp_io_args *args;
1203         ssize_t             result;
1204         int                 refcheck;
1205         ENTRY;
1206
1207         env = cl_env_get(&refcheck);
1208         if (IS_ERR(env))
1209                 RETURN(PTR_ERR(env));
1210
1211         args = vvp_env_args(env, IO_SPLICE);
1212         args->u.splice.via_pipe = pipe;
1213         args->u.splice.via_flags = flags;
1214
1215         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1216         cl_env_put(env, &refcheck);
1217         RETURN(result);
1218 }
1219 #endif
1220
1221 static int ll_lov_recreate(struct inode *inode, obd_id id, obd_seq seq,
1222                            obd_count ost_idx)
1223 {
1224         struct obd_export *exp = ll_i2dtexp(inode);
1225         struct obd_trans_info oti = { 0 };
1226         struct obdo *oa = NULL;
1227         int lsm_size;
1228         int rc = 0;
1229         struct lov_stripe_md *lsm, *lsm2;
1230         ENTRY;
1231
1232         OBDO_ALLOC(oa);
1233         if (oa == NULL)
1234                 RETURN(-ENOMEM);
1235
1236         ll_inode_size_lock(inode, 0);
1237         lsm = ll_i2info(inode)->lli_smd;
1238         if (lsm == NULL)
1239                 GOTO(out, rc = -ENOENT);
1240         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1241                    (lsm->lsm_stripe_count));
1242
1243         OBD_ALLOC_LARGE(lsm2, lsm_size);
1244         if (lsm2 == NULL)
1245                 GOTO(out, rc = -ENOMEM);
1246
1247         oa->o_id = id;
1248         oa->o_seq = seq;
1249         oa->o_nlink = ost_idx;
1250         oa->o_flags |= OBD_FL_RECREATE_OBJS;
1251         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1252         obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1253                                    OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1254         obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
1255         memcpy(lsm2, lsm, lsm_size);
1256         rc = obd_create(NULL, exp, oa, &lsm2, &oti);
1257
1258         OBD_FREE_LARGE(lsm2, lsm_size);
1259         GOTO(out, rc);
1260 out:
1261         ll_inode_size_unlock(inode, 0);
1262         OBDO_FREE(oa);
1263         return rc;
1264 }
1265
1266 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1267 {
1268         struct ll_recreate_obj ucreat;
1269         ENTRY;
1270
1271         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1272                 RETURN(-EPERM);
1273
1274         if (cfs_copy_from_user(&ucreat, (struct ll_recreate_obj *)arg,
1275                                sizeof(struct ll_recreate_obj)))
1276                 RETURN(-EFAULT);
1277
1278         RETURN(ll_lov_recreate(inode, ucreat.lrc_id, 0,
1279                                ucreat.lrc_ost_idx));
1280 }
1281
1282 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1283 {
1284         struct lu_fid fid;
1285         obd_id id;
1286         obd_count ost_idx;
1287         ENTRY;
1288
1289         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1290                 RETURN(-EPERM);
1291
1292         if (cfs_copy_from_user(&fid, (struct lu_fid *)arg,
1293                                sizeof(struct lu_fid)))
1294                 RETURN(-EFAULT);
1295
1296         id = fid_oid(&fid) | ((fid_seq(&fid) & 0xffff) << 32);
1297         ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1298         RETURN(ll_lov_recreate(inode, id, 0, ost_idx));
1299 }
1300
1301 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1302                              int flags, struct lov_user_md *lum, int lum_size)
1303 {
1304         struct lov_stripe_md *lsm;
1305         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1306         int rc = 0;
1307         ENTRY;
1308
1309         ll_inode_size_lock(inode, 0);
1310         lsm = ll_i2info(inode)->lli_smd;
1311         if (lsm) {
1312                 ll_inode_size_unlock(inode, 0);
1313                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1314                        inode->i_ino);
1315                 RETURN(-EEXIST);
1316         }
1317
1318         rc = ll_intent_file_open(file, lum, lum_size, &oit);
1319         if (rc)
1320                 GOTO(out, rc);
1321         rc = oit.d.lustre.it_status;
1322         if (rc < 0)
1323                 GOTO(out_req_free, rc);
1324
1325         ll_release_openhandle(file->f_dentry, &oit);
1326
1327  out:
1328         ll_inode_size_unlock(inode, 0);
1329         ll_intent_release(&oit);
1330         RETURN(rc);
1331 out_req_free:
1332         ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1333         goto out;
1334 }
1335
1336 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1337                              struct lov_mds_md **lmmp, int *lmm_size,
1338                              struct ptlrpc_request **request)
1339 {
1340         struct ll_sb_info *sbi = ll_i2sbi(inode);
1341         struct mdt_body  *body;
1342         struct lov_mds_md *lmm = NULL;
1343         struct ptlrpc_request *req = NULL;
1344         struct md_op_data *op_data;
1345         int rc, lmmsize;
1346
1347         rc = ll_get_max_mdsize(sbi, &lmmsize);
1348         if (rc)
1349                 RETURN(rc);
1350
1351         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1352                                      strlen(filename), lmmsize,
1353                                      LUSTRE_OPC_ANY, NULL);
1354         if (IS_ERR(op_data))
1355                 RETURN(PTR_ERR(op_data));
1356
1357         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1358         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1359         ll_finish_md_op_data(op_data);
1360         if (rc < 0) {
1361                 CDEBUG(D_INFO, "md_getattr_name failed "
1362                        "on %s: rc %d\n", filename, rc);
1363                 GOTO(out, rc);
1364         }
1365
1366         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1367         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1368
1369         lmmsize = body->eadatasize;
1370
1371         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1372                         lmmsize == 0) {
1373                 GOTO(out, rc = -ENODATA);
1374         }
1375
1376         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1377         LASSERT(lmm != NULL);
1378
1379         if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1380             (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1381                 GOTO(out, rc = -EPROTO);
1382         }
1383
1384         /*
1385          * This is coming from the MDS, so is probably in
1386          * little endian.  We convert it to host endian before
1387          * passing it to userspace.
1388          */
1389         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1390                 /* if function called for directory - we should
1391                  * avoid swab not existent lsm objects */
1392                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1393                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1394                         if (S_ISREG(body->mode))
1395                                 lustre_swab_lov_user_md_objects(
1396                                  ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1397                                  ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1398                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1399                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1400                         if (S_ISREG(body->mode))
1401                                 lustre_swab_lov_user_md_objects(
1402                                  ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1403                                  ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1404                 }
1405         }
1406
1407 out:
1408         *lmmp = lmm;
1409         *lmm_size = lmmsize;
1410         *request = req;
1411         return rc;
1412 }
1413
1414 static int ll_lov_setea(struct inode *inode, struct file *file,
1415                             unsigned long arg)
1416 {
1417         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1418         struct lov_user_md  *lump;
1419         int lum_size = sizeof(struct lov_user_md) +
1420                        sizeof(struct lov_user_ost_data);
1421         int rc;
1422         ENTRY;
1423
1424         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1425                 RETURN(-EPERM);
1426
1427         OBD_ALLOC_LARGE(lump, lum_size);
1428         if (lump == NULL) {
1429                 RETURN(-ENOMEM);
1430         }
1431         if (cfs_copy_from_user(lump, (struct lov_user_md  *)arg, lum_size)) {
1432                 OBD_FREE_LARGE(lump, lum_size);
1433                 RETURN(-EFAULT);
1434         }
1435
1436         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1437
1438         OBD_FREE_LARGE(lump, lum_size);
1439         RETURN(rc);
1440 }
1441
1442 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1443                             unsigned long arg)
1444 {
1445         struct lov_user_md_v3 lumv3;
1446         struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1447         struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1448         struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1449         int lum_size;
1450         int rc;
1451         int flags = FMODE_WRITE;
1452         ENTRY;
1453
1454         /* first try with v1 which is smaller than v3 */
1455         lum_size = sizeof(struct lov_user_md_v1);
1456         if (cfs_copy_from_user(lumv1, lumv1p, lum_size))
1457                 RETURN(-EFAULT);
1458
1459         if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1460                 lum_size = sizeof(struct lov_user_md_v3);
1461                 if (cfs_copy_from_user(&lumv3, lumv3p, lum_size))
1462                         RETURN(-EFAULT);
1463         }
1464
1465         rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1466         if (rc == 0) {
1467                  put_user(0, &lumv1p->lmm_stripe_count);
1468                  rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1469                                     0, ll_i2info(inode)->lli_smd,
1470                                     (void *)arg);
1471         }
1472         RETURN(rc);
1473 }
1474
1475 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1476 {
1477         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1478         int rc = -ENODATA;
1479         ENTRY;
1480
1481         if (lsm != NULL)
1482                 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
1483                                    lsm, (void *)arg);
1484         RETURN(rc);
1485 }
1486
1487 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1488 {
1489         struct ll_inode_info   *lli = ll_i2info(inode);
1490         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1491         struct ccc_grouplock    grouplock;
1492         int                     rc;
1493         ENTRY;
1494
1495         if (ll_file_nolock(file))
1496                 RETURN(-EOPNOTSUPP);
1497
1498         cfs_spin_lock(&lli->lli_lock);
1499         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1500                 CWARN("group lock already existed with gid %lu\n",
1501                        fd->fd_grouplock.cg_gid);
1502                 cfs_spin_unlock(&lli->lli_lock);
1503                 RETURN(-EINVAL);
1504         }
1505         LASSERT(fd->fd_grouplock.cg_lock == NULL);
1506         cfs_spin_unlock(&lli->lli_lock);
1507
1508         rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1509                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
1510         if (rc)
1511                 RETURN(rc);
1512
1513         cfs_spin_lock(&lli->lli_lock);
1514         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1515                 cfs_spin_unlock(&lli->lli_lock);
1516                 CERROR("another thread just won the race\n");
1517                 cl_put_grouplock(&grouplock);
1518                 RETURN(-EINVAL);
1519         }
1520
1521         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1522         fd->fd_grouplock = grouplock;
1523         cfs_spin_unlock(&lli->lli_lock);
1524
1525         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1526         RETURN(0);
1527 }
1528
1529 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1530 {
1531         struct ll_inode_info   *lli = ll_i2info(inode);
1532         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1533         struct ccc_grouplock    grouplock;
1534         ENTRY;
1535
1536         cfs_spin_lock(&lli->lli_lock);
1537         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1538                 cfs_spin_unlock(&lli->lli_lock);
1539                 CWARN("no group lock held\n");
1540                 RETURN(-EINVAL);
1541         }
1542         LASSERT(fd->fd_grouplock.cg_lock != NULL);
1543
1544         if (fd->fd_grouplock.cg_gid != arg) {
1545                 CWARN("group lock %lu doesn't match current id %lu\n",
1546                        arg, fd->fd_grouplock.cg_gid);
1547                 cfs_spin_unlock(&lli->lli_lock);
1548                 RETURN(-EINVAL);
1549         }
1550
1551         grouplock = fd->fd_grouplock;
1552         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1553         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1554         cfs_spin_unlock(&lli->lli_lock);
1555
1556         cl_put_grouplock(&grouplock);
1557         CDEBUG(D_INFO, "group lock %lu released\n", arg);
1558         RETURN(0);
1559 }
1560
1561 /**
1562  * Close inode open handle
1563  *
1564  * \param dentry [in]     dentry which contains the inode
1565  * \param it     [in,out] intent which contains open info and result
1566  *
1567  * \retval 0     success
1568  * \retval <0    failure
1569  */
1570 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1571 {
1572         struct inode *inode = dentry->d_inode;
1573         struct obd_client_handle *och;
1574         int rc;
1575         ENTRY;
1576
1577         LASSERT(inode);
1578
1579         /* Root ? Do nothing. */
1580         if (dentry->d_inode->i_sb->s_root == dentry)
1581                 RETURN(0);
1582
1583         /* No open handle to close? Move away */
1584         if (!it_disposition(it, DISP_OPEN_OPEN))
1585                 RETURN(0);
1586
1587         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1588
1589         OBD_ALLOC(och, sizeof(*och));
1590         if (!och)
1591                 GOTO(out, rc = -ENOMEM);
1592
1593         ll_och_fill(ll_i2sbi(inode)->ll_md_exp,
1594                     ll_i2info(inode), it, och);
1595
1596         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1597                                        inode, och);
1598  out:
1599         /* this one is in place of ll_file_open */
1600         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1601                 ptlrpc_req_finished(it->d.lustre.it_data);
1602                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1603         }
1604         RETURN(rc);
1605 }
1606
1607 /**
1608  * Get size for inode for which FIEMAP mapping is requested.
1609  * Make the FIEMAP get_info call and returns the result.
1610  */
1611 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1612               int num_bytes)
1613 {
1614         struct obd_export *exp = ll_i2dtexp(inode);
1615         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1616         struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1617         int vallen = num_bytes;
1618         int rc;
1619         ENTRY;
1620
1621         /* Checks for fiemap flags */
1622         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1623                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1624                 return -EBADR;
1625         }
1626
1627         /* Check for FIEMAP_FLAG_SYNC */
1628         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1629                 rc = filemap_fdatawrite(inode->i_mapping);
1630                 if (rc)
1631                         return rc;
1632         }
1633
1634         /* If the stripe_count > 1 and the application does not understand
1635          * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1636          */
1637         if (lsm->lsm_stripe_count > 1 &&
1638             !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1639                 return -EOPNOTSUPP;
1640
1641         fm_key.oa.o_id = lsm->lsm_object_id;
1642         fm_key.oa.o_seq = lsm->lsm_object_seq;
1643         fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1644
1645         obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE);
1646         obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid);
1647         /* If filesize is 0, then there would be no objects for mapping */
1648         if (fm_key.oa.o_size == 0) {
1649                 fiemap->fm_mapped_extents = 0;
1650                 RETURN(0);
1651         }
1652
1653         memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1654
1655         rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen,
1656                           fiemap, lsm);
1657         if (rc)
1658                 CERROR("obd_get_info failed: rc = %d\n", rc);
1659
1660         RETURN(rc);
1661 }
1662
1663 int ll_fid2path(struct obd_export *exp, void *arg)
1664 {
1665         struct getinfo_fid2path *gfout, *gfin;
1666         int outsize, rc;
1667         ENTRY;
1668
1669         /* Need to get the buflen */
1670         OBD_ALLOC_PTR(gfin);
1671         if (gfin == NULL)
1672                 RETURN(-ENOMEM);
1673         if (cfs_copy_from_user(gfin, arg, sizeof(*gfin))) {
1674                 OBD_FREE_PTR(gfin);
1675                 RETURN(-EFAULT);
1676         }
1677
1678         outsize = sizeof(*gfout) + gfin->gf_pathlen;
1679         OBD_ALLOC(gfout, outsize);
1680         if (gfout == NULL) {
1681                 OBD_FREE_PTR(gfin);
1682                 RETURN(-ENOMEM);
1683         }
1684         memcpy(gfout, gfin, sizeof(*gfout));
1685         OBD_FREE_PTR(gfin);
1686
1687         /* Call mdc_iocontrol */
1688         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1689         if (rc)
1690                 GOTO(gf_free, rc);
1691         if (cfs_copy_to_user(arg, gfout, outsize))
1692                 rc = -EFAULT;
1693
1694 gf_free:
1695         OBD_FREE(gfout, outsize);
1696         RETURN(rc);
1697 }
1698
1699 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1700 {
1701         struct ll_user_fiemap *fiemap_s;
1702         size_t num_bytes, ret_bytes;
1703         unsigned int extent_count;
1704         int rc = 0;
1705
1706         /* Get the extent count so we can calculate the size of
1707          * required fiemap buffer */
1708         if (get_user(extent_count,
1709             &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1710                 RETURN(-EFAULT);
1711         num_bytes = sizeof(*fiemap_s) + (extent_count *
1712                                          sizeof(struct ll_fiemap_extent));
1713
1714         OBD_ALLOC_LARGE(fiemap_s, num_bytes);
1715         if (fiemap_s == NULL)
1716                 RETURN(-ENOMEM);
1717
1718         /* get the fiemap value */
1719         if (copy_from_user(fiemap_s,(struct ll_user_fiemap __user *)arg,
1720                            sizeof(*fiemap_s)))
1721                 GOTO(error, rc = -EFAULT);
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         if (extent_count) {
1727                 if (copy_from_user(&fiemap_s->fm_extents[0],
1728                     (char __user *)arg + sizeof(*fiemap_s),
1729                     sizeof(struct ll_fiemap_extent)))
1730                         GOTO(error, rc = -EFAULT);
1731         }
1732
1733         rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1734         if (rc)
1735                 GOTO(error, rc);
1736
1737         ret_bytes = sizeof(struct ll_user_fiemap);
1738
1739         if (extent_count != 0)
1740                 ret_bytes += (fiemap_s->fm_mapped_extents *
1741                                  sizeof(struct ll_fiemap_extent));
1742
1743         if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1744                 rc = -EFAULT;
1745
1746 error:
1747         OBD_FREE_LARGE(fiemap_s, num_bytes);
1748         RETURN(rc);
1749 }
1750
1751 /*
1752  * Read the data_version for inode.
1753  *
1754  * This value is computed using stripe object version on OST.
1755  * Version is computed using server side locking.
1756  *
1757  * @param extent_lock  Take extent lock. Not needed if a process is already
1758  *                     holding the OST object group locks.
1759  */
1760 static int ll_data_version(struct inode *inode, __u64 *data_version,
1761                            int extent_lock)
1762 {
1763         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1764         struct ll_sb_info    *sbi = ll_i2sbi(inode);
1765         struct obdo          *obdo = NULL;
1766         int                   rc;
1767         ENTRY;
1768
1769         /* If no stripe, we consider version is 0. */
1770         if (!lsm) {
1771                 *data_version = 0;
1772                 CDEBUG(D_INODE, "No object for inode\n");
1773                 RETURN(0);
1774         }
1775
1776         OBD_ALLOC_PTR(obdo);
1777         if (obdo == NULL)
1778                 RETURN(-ENOMEM);
1779
1780         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, obdo, 0, extent_lock);
1781         if (!rc) {
1782                 if (!(obdo->o_valid & OBD_MD_FLDATAVERSION))
1783                         rc = -EOPNOTSUPP;
1784                 else
1785                         *data_version = obdo->o_data_version;
1786         }
1787
1788         OBD_FREE_PTR(obdo);
1789
1790         RETURN(rc);
1791 }
1792
1793 long ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1794 {
1795         struct inode *inode = file->f_dentry->d_inode;
1796         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1797         int flags;
1798
1799         ENTRY;
1800
1801         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1802                inode->i_generation, inode, cmd);
1803         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1804
1805         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1806         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1807                 RETURN(-ENOTTY);
1808
1809         switch(cmd) {
1810         case LL_IOC_GETFLAGS:
1811                 /* Get the current value of the file flags */
1812                 return put_user(fd->fd_flags, (int *)arg);
1813         case LL_IOC_SETFLAGS:
1814         case LL_IOC_CLRFLAGS:
1815                 /* Set or clear specific file flags */
1816                 /* XXX This probably needs checks to ensure the flags are
1817                  *     not abused, and to handle any flag side effects.
1818                  */
1819                 if (get_user(flags, (int *) arg))
1820                         RETURN(-EFAULT);
1821
1822                 if (cmd == LL_IOC_SETFLAGS) {
1823                         if ((flags & LL_FILE_IGNORE_LOCK) &&
1824                             !(file->f_flags & O_DIRECT)) {
1825                                 CERROR("%s: unable to disable locking on "
1826                                        "non-O_DIRECT file\n", current->comm);
1827                                 RETURN(-EINVAL);
1828                         }
1829
1830                         fd->fd_flags |= flags;
1831                 } else {
1832                         fd->fd_flags &= ~flags;
1833                 }
1834                 RETURN(0);
1835         case LL_IOC_LOV_SETSTRIPE:
1836                 RETURN(ll_lov_setstripe(inode, file, arg));
1837         case LL_IOC_LOV_SETEA:
1838                 RETURN(ll_lov_setea(inode, file, arg));
1839         case LL_IOC_LOV_GETSTRIPE:
1840                 RETURN(ll_lov_getstripe(inode, arg));
1841         case LL_IOC_RECREATE_OBJ:
1842                 RETURN(ll_lov_recreate_obj(inode, arg));
1843         case LL_IOC_RECREATE_FID:
1844                 RETURN(ll_lov_recreate_fid(inode, arg));
1845         case FSFILT_IOC_FIEMAP:
1846                 RETURN(ll_ioctl_fiemap(inode, arg));
1847         case FSFILT_IOC_GETFLAGS:
1848         case FSFILT_IOC_SETFLAGS:
1849                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1850         case FSFILT_IOC_GETVERSION_OLD:
1851         case FSFILT_IOC_GETVERSION:
1852                 RETURN(put_user(inode->i_generation, (int *)arg));
1853         case LL_IOC_GROUP_LOCK:
1854                 RETURN(ll_get_grouplock(inode, file, arg));
1855         case LL_IOC_GROUP_UNLOCK:
1856                 RETURN(ll_put_grouplock(inode, file, arg));
1857         case IOC_OBD_STATFS:
1858                 RETURN(ll_obd_statfs(inode, (void *)arg));
1859
1860         /* We need to special case any other ioctls we want to handle,
1861          * to send them to the MDS/OST as appropriate and to properly
1862          * network encode the arg field.
1863         case FSFILT_IOC_SETVERSION_OLD:
1864         case FSFILT_IOC_SETVERSION:
1865         */
1866         case LL_IOC_FLUSHCTX:
1867                 RETURN(ll_flush_ctx(inode));
1868         case LL_IOC_PATH2FID: {
1869                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1870                                      sizeof(struct lu_fid)))
1871                         RETURN(-EFAULT);
1872
1873                 RETURN(0);
1874         }
1875         case OBD_IOC_FID2PATH:
1876                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1877         case LL_IOC_DATA_VERSION: {
1878                 struct ioc_data_version idv;
1879                 int rc;
1880
1881                 if (cfs_copy_from_user(&idv, (char *)arg, sizeof(idv)))
1882                         RETURN(-EFAULT);
1883
1884                 rc = ll_data_version(inode, &idv.idv_version,
1885                                      !(idv.idv_flags & LL_DV_NOFLUSH));
1886
1887                 if (rc == 0 &&
1888                     cfs_copy_to_user((char *) arg, &idv, sizeof(idv)))
1889                         RETURN(-EFAULT);
1890
1891                 RETURN(rc);
1892         }
1893
1894         case LL_IOC_GET_MDTIDX: {
1895                 int mdtidx;
1896
1897                 mdtidx = ll_get_mdt_idx(inode);
1898                 if (mdtidx < 0)
1899                         RETURN(mdtidx);
1900
1901                 if (put_user((int)mdtidx, (int*)arg))
1902                         RETURN(-EFAULT);
1903
1904                 RETURN(0);
1905         }
1906         case OBD_IOC_GETDTNAME:
1907         case OBD_IOC_GETMDNAME:
1908                 RETURN(ll_get_obd_name(inode, cmd, arg));
1909         default: {
1910                 int err;
1911
1912                 if (LLIOC_STOP ==
1913                     ll_iocontrol_call(inode, file, cmd, arg, &err))
1914                         RETURN(err);
1915
1916                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1917                                      (void *)arg));
1918         }
1919         }
1920 }
1921
1922 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1923 {
1924         struct inode *inode = file->f_dentry->d_inode;
1925         loff_t retval;
1926         ENTRY;
1927         retval = offset + ((origin == 2) ? i_size_read(inode) :
1928                            (origin == 1) ? file->f_pos : 0);
1929         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%llu=%#llx(%s)\n",
1930                inode->i_ino, inode->i_generation, inode, retval, retval,
1931                origin == 2 ? "SEEK_END": origin == 1 ? "SEEK_CUR" : "SEEK_SET");
1932         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
1933
1934         if (origin == 2) { /* SEEK_END */
1935                 int rc;
1936
1937                 rc = ll_glimpse_size(inode);
1938                 if (rc != 0)
1939                         RETURN(rc);
1940
1941                 offset += i_size_read(inode);
1942         } else if (origin == 1) { /* SEEK_CUR */
1943                 offset += file->f_pos;
1944         }
1945
1946         retval = -EINVAL;
1947         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1948                 if (offset != file->f_pos) {
1949                         file->f_pos = offset;
1950                 }
1951                 retval = offset;
1952         }
1953
1954         RETURN(retval);
1955 }
1956
1957 #ifdef HAVE_FLUSH_OWNER_ID
1958 int ll_flush(struct file *file, fl_owner_t id)
1959 #else
1960 int ll_flush(struct file *file)
1961 #endif
1962 {
1963         struct inode *inode = file->f_dentry->d_inode;
1964         struct ll_inode_info *lli = ll_i2info(inode);
1965         struct lov_stripe_md *lsm = lli->lli_smd;
1966         int rc, err;
1967
1968         LASSERT(!S_ISDIR(inode->i_mode));
1969
1970         /* the application should know write failure already. */
1971         if (lli->lli_write_rc)
1972                 return 0;
1973
1974         /* catch async errors that were recorded back when async writeback
1975          * failed for pages in this mapping. */
1976         rc = lli->lli_async_rc;
1977         lli->lli_async_rc = 0;
1978         if (lsm) {
1979                 err = lov_test_and_clear_async_rc(lsm);
1980                 if (rc == 0)
1981                         rc = err;
1982         }
1983
1984         return rc ? -EIO : 0;
1985 }
1986
1987 /**
1988  * Called to make sure a portion of file has been written out.
1989  * if @local_only is not true, it will send OST_SYNC RPCs to ost.
1990  *
1991  * Return how many pages have been written.
1992  */
1993 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
1994                        enum cl_fsync_mode mode)
1995 {
1996         struct cl_env_nest nest;
1997         struct lu_env *env;
1998         struct cl_io *io;
1999         struct obd_capa *capa = NULL;
2000         struct cl_fsync_io *fio;
2001         int result;
2002         ENTRY;
2003
2004         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2005             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2006                 RETURN(-EINVAL);
2007
2008         env = cl_env_nested_get(&nest);
2009         if (IS_ERR(env))
2010                 RETURN(PTR_ERR(env));
2011
2012         capa = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
2013
2014         io = ccc_env_thread_io(env);
2015         io->ci_obj = cl_i2info(inode)->lli_clob;
2016
2017         /* initialize parameters for sync */
2018         fio = &io->u.ci_fsync;
2019         fio->fi_capa = capa;
2020         fio->fi_start = start;
2021         fio->fi_end = end;
2022         fio->fi_fid = ll_inode2fid(inode);
2023         fio->fi_mode = mode;
2024         fio->fi_nr_written = 0;
2025
2026         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2027                 result = cl_io_loop(env, io);
2028         else
2029                 result = io->ci_result;
2030         if (result == 0)
2031                 result = fio->fi_nr_written;
2032         cl_io_fini(env, io);
2033         cl_env_nested_put(&nest, env);
2034
2035         capa_put(capa);
2036
2037         RETURN(result);
2038 }
2039
2040 #ifdef HAVE_FILE_FSYNC_4ARGS
2041 int ll_fsync(struct file *file, loff_t start, loff_t end, int data)
2042 #elif defined(HAVE_FILE_FSYNC_2ARGS)
2043 int ll_fsync(struct file *file, int data)
2044 #else
2045 int ll_fsync(struct file *file, struct dentry *dentry, int data)
2046 #endif
2047 {
2048         struct inode *inode = file->f_dentry->d_inode;
2049         struct ll_inode_info *lli = ll_i2info(inode);
2050         struct lov_stripe_md *lsm = lli->lli_smd;
2051         struct ptlrpc_request *req;
2052         struct obd_capa *oc;
2053         int rc, err;
2054         ENTRY;
2055         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2056                inode->i_generation, inode);
2057         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2058
2059         /* fsync's caller has already called _fdata{sync,write}, we want
2060          * that IO to finish before calling the osc and mdc sync methods */
2061         rc = filemap_fdatawait(inode->i_mapping);
2062
2063         /* catch async errors that were recorded back when async writeback
2064          * failed for pages in this mapping. */
2065         if (!S_ISDIR(inode->i_mode)) {
2066                 err = lli->lli_async_rc;
2067                 lli->lli_async_rc = 0;
2068                 if (rc == 0)
2069                         rc = err;
2070                 if (lsm) {
2071                         err = lov_test_and_clear_async_rc(lsm);
2072                         if (rc == 0)
2073                                 rc = err;
2074                 }
2075         }
2076
2077         oc = ll_mdscapa_get(inode);
2078         err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
2079                       &req);
2080         capa_put(oc);
2081         if (!rc)
2082                 rc = err;
2083         if (!err)
2084                 ptlrpc_req_finished(req);
2085
2086         if (data && lsm) {
2087                 err = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
2088                                          CL_FSYNC_ALL);
2089                 if (rc == 0 && err < 0)
2090                         rc = err;
2091                 lli->lli_write_rc = rc < 0 ? rc : 0;
2092         }
2093
2094         RETURN(rc);
2095 }
2096
2097 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2098 {
2099         struct inode *inode = file->f_dentry->d_inode;
2100         struct ll_sb_info *sbi = ll_i2sbi(inode);
2101         struct ldlm_enqueue_info einfo = { .ei_type = LDLM_FLOCK,
2102                                            .ei_cb_cp =ldlm_flock_completion_ast,
2103                                            .ei_cbdata = file_lock };
2104         struct md_op_data *op_data;
2105         struct lustre_handle lockh = {0};
2106         ldlm_policy_data_t flock = {{0}};
2107         int flags = 0;
2108         int rc;
2109         ENTRY;
2110
2111         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
2112                inode->i_ino, file_lock);
2113
2114         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2115
2116         if (file_lock->fl_flags & FL_FLOCK) {
2117                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2118                 /* flocks are whole-file locks */
2119                 flock.l_flock.end = OFFSET_MAX;
2120                 /* For flocks owner is determined by the local file desctiptor*/
2121                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
2122         } else if (file_lock->fl_flags & FL_POSIX) {
2123                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2124                 flock.l_flock.start = file_lock->fl_start;
2125                 flock.l_flock.end = file_lock->fl_end;
2126         } else {
2127                 RETURN(-EINVAL);
2128         }
2129         flock.l_flock.pid = file_lock->fl_pid;
2130
2131         /* Somewhat ugly workaround for svc lockd.
2132          * lockd installs custom fl_lmops->fl_compare_owner that checks
2133          * for the fl_owner to be the same (which it always is on local node
2134          * I guess between lockd processes) and then compares pid.
2135          * As such we assign pid to the owner field to make it all work,
2136          * conflict with normal locks is unlikely since pid space and
2137          * pointer space for current->files are not intersecting */
2138         if (file_lock->fl_lmops && file_lock->fl_lmops->fl_compare_owner)
2139                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2140
2141         switch (file_lock->fl_type) {
2142         case F_RDLCK:
2143                 einfo.ei_mode = LCK_PR;
2144                 break;
2145         case F_UNLCK:
2146                 /* An unlock request may or may not have any relation to
2147                  * existing locks so we may not be able to pass a lock handle
2148                  * via a normal ldlm_lock_cancel() request. The request may even
2149                  * unlock a byte range in the middle of an existing lock. In
2150                  * order to process an unlock request we need all of the same
2151                  * information that is given with a normal read or write record
2152                  * lock request. To avoid creating another ldlm unlock (cancel)
2153                  * message we'll treat a LCK_NL flock request as an unlock. */
2154                 einfo.ei_mode = LCK_NL;
2155                 break;
2156         case F_WRLCK:
2157                 einfo.ei_mode = LCK_PW;
2158                 break;
2159         default:
2160                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n",
2161                         file_lock->fl_type);
2162                 RETURN (-ENOTSUPP);
2163         }
2164
2165         switch (cmd) {
2166         case F_SETLKW:
2167 #ifdef F_SETLKW64
2168         case F_SETLKW64:
2169 #endif
2170                 flags = 0;
2171                 break;
2172         case F_SETLK:
2173 #ifdef F_SETLK64
2174         case F_SETLK64:
2175 #endif
2176                 flags = LDLM_FL_BLOCK_NOWAIT;
2177                 break;
2178         case F_GETLK:
2179 #ifdef F_GETLK64
2180         case F_GETLK64:
2181 #endif
2182                 flags = LDLM_FL_TEST_LOCK;
2183                 /* Save the old mode so that if the mode in the lock changes we
2184                  * can decrement the appropriate reader or writer refcount. */
2185                 file_lock->fl_type = einfo.ei_mode;
2186                 break;
2187         default:
2188                 CERROR("unknown fcntl lock command: %d\n", cmd);
2189                 RETURN (-EINVAL);
2190         }
2191
2192         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2193                                      LUSTRE_OPC_ANY, NULL);
2194         if (IS_ERR(op_data))
2195                 RETURN(PTR_ERR(op_data));
2196
2197         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
2198                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
2199                flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2200
2201         rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2202                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2203
2204         ll_finish_md_op_data(op_data);
2205
2206         if ((file_lock->fl_flags & FL_FLOCK) &&
2207             (rc == 0 || file_lock->fl_type == F_UNLCK))
2208                 flock_lock_file_wait(file, file_lock);
2209         if ((file_lock->fl_flags & FL_POSIX) &&
2210             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2211             !(flags & LDLM_FL_TEST_LOCK))
2212                 posix_lock_file_wait(file, file_lock);
2213
2214         RETURN(rc);
2215 }
2216
2217 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2218 {
2219         ENTRY;
2220
2221         RETURN(-ENOSYS);
2222 }
2223
2224 /**
2225  * test if some locks matching bits and l_req_mode are acquired
2226  * - bits can be in different locks
2227  * - if found clear the common lock bits in *bits
2228  * - the bits not found, are kept in *bits
2229  * \param inode [IN]
2230  * \param bits [IN] searched lock bits [IN]
2231  * \param l_req_mode [IN] searched lock mode
2232  * \retval boolean, true iff all bits are found
2233  */
2234 int ll_have_md_lock(struct inode *inode, __u64 *bits,  ldlm_mode_t l_req_mode)
2235 {
2236         struct lustre_handle lockh;
2237         ldlm_policy_data_t policy;
2238         ldlm_mode_t mode = (l_req_mode == LCK_MINMODE) ?
2239                                 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2240         struct lu_fid *fid;
2241         int flags;
2242         int i;
2243         ENTRY;
2244
2245         if (!inode)
2246                RETURN(0);
2247
2248         fid = &ll_i2info(inode)->lli_fid;
2249         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2250                ldlm_lockname[mode]);
2251
2252         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2253         for (i = 0; i < MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2254                 policy.l_inodebits.bits = *bits & (1 << i);
2255                 if (policy.l_inodebits.bits == 0)
2256                         continue;
2257
2258                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2259                                   &policy, mode, &lockh)) {
2260                         struct ldlm_lock *lock;
2261
2262                         lock = ldlm_handle2lock(&lockh);
2263                         if (lock) {
2264                                 *bits &=
2265                                       ~(lock->l_policy_data.l_inodebits.bits);
2266                                 LDLM_LOCK_PUT(lock);
2267                         } else {
2268                                 *bits &= ~policy.l_inodebits.bits;
2269                         }
2270                 }
2271         }
2272         RETURN(*bits == 0);
2273 }
2274
2275 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2276                             struct lustre_handle *lockh)
2277 {
2278         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2279         struct lu_fid *fid;
2280         ldlm_mode_t rc;
2281         int flags;
2282         ENTRY;
2283
2284         fid = &ll_i2info(inode)->lli_fid;
2285         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2286
2287         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
2288         rc = md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2289                            LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2290         RETURN(rc);
2291 }
2292
2293 static int ll_inode_revalidate_fini(struct inode *inode, int rc) {
2294         if (rc == -ENOENT) { /* Already unlinked. Just update nlink
2295                               * and return success */
2296                 inode->i_nlink = 0;
2297                 /* This path cannot be hit for regular files unless in
2298                  * case of obscure races, so no need to to validate
2299                  * size. */
2300                 if (!S_ISREG(inode->i_mode) &&
2301                     !S_ISDIR(inode->i_mode))
2302                         return 0;
2303         }
2304
2305         if (rc) {
2306                 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2307                 return -abs(rc);
2308
2309         }
2310
2311         return 0;
2312 }
2313
2314 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2315                              __u64 ibits)
2316 {
2317         struct inode *inode = dentry->d_inode;
2318         struct ptlrpc_request *req = NULL;
2319         struct obd_export *exp;
2320         int rc = 0;
2321         ENTRY;
2322
2323         if (!inode) {
2324                 CERROR("REPORT THIS LINE TO PETER\n");
2325                 RETURN(0);
2326         }
2327
2328         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2329                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2330
2331         exp = ll_i2mdexp(inode);
2332
2333         /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2334          *      But under CMD case, it caused some lock issues, should be fixed
2335          *      with new CMD ibits lock. See bug 12718 */
2336         if (exp->exp_connect_flags & OBD_CONNECT_ATTRFID) {
2337                 struct lookup_intent oit = { .it_op = IT_GETATTR };
2338                 struct md_op_data *op_data;
2339
2340                 if (ibits == MDS_INODELOCK_LOOKUP)
2341                         oit.it_op = IT_LOOKUP;
2342
2343                 /* Call getattr by fid, so do not provide name at all. */
2344                 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2345                                              dentry->d_inode, NULL, 0, 0,
2346                                              LUSTRE_OPC_ANY, NULL);
2347                 if (IS_ERR(op_data))
2348                         RETURN(PTR_ERR(op_data));
2349
2350                 oit.it_create_mode |= M_CHECK_STALE;
2351                 rc = md_intent_lock(exp, op_data, NULL, 0,
2352                                     /* we are not interested in name
2353                                        based lookup */
2354                                     &oit, 0, &req,
2355                                     ll_md_blocking_ast, 0);
2356                 ll_finish_md_op_data(op_data);
2357                 oit.it_create_mode &= ~M_CHECK_STALE;
2358                 if (rc < 0) {
2359                         rc = ll_inode_revalidate_fini(inode, rc);
2360                         GOTO (out, rc);
2361                 }
2362
2363                 rc = ll_revalidate_it_finish(req, &oit, dentry);
2364                 if (rc != 0) {
2365                         ll_intent_release(&oit);
2366                         GOTO(out, rc);
2367                 }
2368
2369                 /* Unlinked? Unhash dentry, so it is not picked up later by
2370                    do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2371                    here to preserve get_cwd functionality on 2.6.
2372                    Bug 10503 */
2373                 if (!dentry->d_inode->i_nlink)
2374                         d_lustre_invalidate(dentry);
2375
2376                 ll_lookup_finish_locks(&oit, dentry);
2377         } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
2378                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2379                 obd_valid valid = OBD_MD_FLGETATTR;
2380                 struct md_op_data *op_data;
2381                 int ealen = 0;
2382
2383                 if (S_ISREG(inode->i_mode)) {
2384                         rc = ll_get_max_mdsize(sbi, &ealen);
2385                         if (rc)
2386                                 RETURN(rc);
2387                         valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2388                 }
2389
2390                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2391                                              0, ealen, LUSTRE_OPC_ANY,
2392                                              NULL);
2393                 if (IS_ERR(op_data))
2394                         RETURN(PTR_ERR(op_data));
2395
2396                 op_data->op_valid = valid;
2397                 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2398                  * capa for this inode. Because we only keep capas of dirs
2399                  * fresh. */
2400                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2401                 ll_finish_md_op_data(op_data);
2402                 if (rc) {
2403                         rc = ll_inode_revalidate_fini(inode, rc);
2404                         RETURN(rc);
2405                 }
2406
2407                 rc = ll_prep_inode(&inode, req, NULL);
2408         }
2409 out:
2410         ptlrpc_req_finished(req);
2411         return rc;
2412 }
2413
2414 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2415                            __u64 ibits)
2416 {
2417         struct inode *inode = dentry->d_inode;
2418         int rc;
2419         ENTRY;
2420
2421         rc = __ll_inode_revalidate_it(dentry, it, ibits);
2422
2423         /* if object not yet allocated, don't validate size */
2424         if (rc == 0 && ll_i2info(dentry->d_inode)->lli_smd == NULL) {
2425                 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2426                 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2427                 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2428                 RETURN(0);
2429         }
2430
2431         /* ll_glimpse_size will prefer locally cached writes if they extend
2432          * the file */
2433
2434         if (rc == 0)
2435                 rc = ll_glimpse_size(inode);
2436
2437         RETURN(rc);
2438 }
2439
2440 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2441                   struct lookup_intent *it, struct kstat *stat)
2442 {
2443         struct inode *inode = de->d_inode;
2444         struct ll_sb_info *sbi = ll_i2sbi(inode);
2445         struct ll_inode_info *lli = ll_i2info(inode);
2446         int res = 0;
2447
2448         res = ll_inode_revalidate_it(de, it, MDS_INODELOCK_UPDATE |
2449                                              MDS_INODELOCK_LOOKUP);
2450         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
2451
2452         if (res)
2453                 return res;
2454
2455         stat->dev = inode->i_sb->s_dev;
2456         if (ll_need_32bit_api(sbi))
2457                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
2458         else
2459                 stat->ino = inode->i_ino;
2460         stat->mode = inode->i_mode;
2461         stat->nlink = inode->i_nlink;
2462         stat->uid = inode->i_uid;
2463         stat->gid = inode->i_gid;
2464         stat->rdev = kdev_t_to_nr(inode->i_rdev);
2465         stat->atime = inode->i_atime;
2466         stat->mtime = inode->i_mtime;
2467         stat->ctime = inode->i_ctime;
2468 #ifdef HAVE_INODE_BLKSIZE
2469         stat->blksize = inode->i_blksize;
2470 #else
2471         stat->blksize = 1 << inode->i_blkbits;
2472 #endif
2473
2474         stat->size = i_size_read(inode);
2475         stat->blocks = inode->i_blocks;
2476
2477         return 0;
2478 }
2479 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2480 {
2481         struct lookup_intent it = { .it_op = IT_GETATTR };
2482
2483         return ll_getattr_it(mnt, de, &it, stat);
2484 }
2485
2486 #ifdef HAVE_LINUX_FIEMAP_H
2487 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2488                 __u64 start, __u64 len)
2489 {
2490         int rc;
2491         size_t num_bytes;
2492         struct ll_user_fiemap *fiemap;
2493         unsigned int extent_count = fieinfo->fi_extents_max;
2494
2495         num_bytes = sizeof(*fiemap) + (extent_count *
2496                                        sizeof(struct ll_fiemap_extent));
2497         OBD_ALLOC_LARGE(fiemap, num_bytes);
2498
2499         if (fiemap == NULL)
2500                 RETURN(-ENOMEM);
2501
2502         fiemap->fm_flags = fieinfo->fi_flags;
2503         fiemap->fm_extent_count = fieinfo->fi_extents_max;
2504         fiemap->fm_start = start;
2505         fiemap->fm_length = len;
2506         memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2507                sizeof(struct ll_fiemap_extent));
2508
2509         rc = ll_do_fiemap(inode, fiemap, num_bytes);
2510
2511         fieinfo->fi_flags = fiemap->fm_flags;
2512         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2513         memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2514                fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2515
2516         OBD_FREE_LARGE(fiemap, num_bytes);
2517         return rc;
2518 }
2519 #endif
2520
2521
2522 static int
2523 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2524 lustre_check_acl(struct inode *inode, int mask, unsigned int flags)
2525 #else
2526 lustre_check_acl(struct inode *inode, int mask)
2527 #endif
2528 {
2529 #ifdef CONFIG_FS_POSIX_ACL
2530         struct ll_inode_info *lli = ll_i2info(inode);
2531         struct posix_acl *acl;
2532         int rc;
2533         ENTRY;
2534
2535 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2536         if (flags & IPERM_FLAG_RCU)
2537                 return -ECHILD;
2538 #endif
2539         cfs_spin_lock(&lli->lli_lock);
2540         acl = posix_acl_dup(lli->lli_posix_acl);
2541         cfs_spin_unlock(&lli->lli_lock);
2542
2543         if (!acl)
2544                 RETURN(-EAGAIN);
2545
2546         rc = posix_acl_permission(inode, acl, mask);
2547         posix_acl_release(acl);
2548
2549         RETURN(rc);
2550 #else
2551         return -EAGAIN;
2552 #endif
2553 }
2554
2555 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2556 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
2557 #else
2558 # ifdef HAVE_INODE_PERMISION_2ARGS
2559 int ll_inode_permission(struct inode *inode, int mask)
2560 # else
2561 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2562 # endif
2563 #endif
2564 {
2565         int rc = 0;
2566         ENTRY;
2567
2568 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
2569         if (flags & IPERM_FLAG_RCU)
2570                 return -ECHILD;
2571 #endif
2572
2573        /* as root inode are NOT getting validated in lookup operation,
2574         * need to do it before permission check. */
2575
2576         if (inode == inode->i_sb->s_root->d_inode) {
2577                 struct lookup_intent it = { .it_op = IT_LOOKUP };
2578
2579                 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2580                                               MDS_INODELOCK_LOOKUP);
2581                 if (rc)
2582                         RETURN(rc);
2583         }
2584
2585         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2586                inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2587
2588         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2589                 return lustre_check_remote_perm(inode, mask);
2590
2591         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2592         rc = ll_generic_permission(inode, mask, flags, lustre_check_acl);
2593
2594         RETURN(rc);
2595 }
2596
2597 #ifdef HAVE_FILE_READV
2598 #define READ_METHOD readv
2599 #define READ_FUNCTION ll_file_readv
2600 #define WRITE_METHOD writev
2601 #define WRITE_FUNCTION ll_file_writev
2602 #else
2603 #define READ_METHOD aio_read
2604 #define READ_FUNCTION ll_file_aio_read
2605 #define WRITE_METHOD aio_write
2606 #define WRITE_FUNCTION ll_file_aio_write
2607 #endif
2608
2609 /* -o localflock - only provides locally consistent flock locks */
2610 struct file_operations ll_file_operations = {
2611         .read           = ll_file_read,
2612         .READ_METHOD    = READ_FUNCTION,
2613         .write          = ll_file_write,
2614         .WRITE_METHOD   = WRITE_FUNCTION,
2615         .unlocked_ioctl = ll_file_ioctl,
2616         .open           = ll_file_open,
2617         .release        = ll_file_release,
2618         .mmap           = ll_file_mmap,
2619         .llseek         = ll_file_seek,
2620 #ifdef HAVE_KERNEL_SENDFILE
2621         .sendfile       = ll_file_sendfile,
2622 #endif
2623 #ifdef HAVE_KERNEL_SPLICE_READ
2624         .splice_read    = ll_file_splice_read,
2625 #endif
2626         .fsync          = ll_fsync,
2627         .flush          = ll_flush
2628 };
2629
2630 struct file_operations ll_file_operations_flock = {
2631         .read           = ll_file_read,
2632         .READ_METHOD    = READ_FUNCTION,
2633         .write          = ll_file_write,
2634         .WRITE_METHOD   = WRITE_FUNCTION,
2635         .unlocked_ioctl = ll_file_ioctl,
2636         .open           = ll_file_open,
2637         .release        = ll_file_release,
2638         .mmap           = ll_file_mmap,
2639         .llseek         = ll_file_seek,
2640 #ifdef HAVE_KERNEL_SENDFILE
2641         .sendfile       = ll_file_sendfile,
2642 #endif
2643 #ifdef HAVE_KERNEL_SPLICE_READ
2644         .splice_read    = ll_file_splice_read,
2645 #endif
2646         .fsync          = ll_fsync,
2647         .flush          = ll_flush,
2648         .flock          = ll_file_flock,
2649         .lock           = ll_file_flock
2650 };
2651
2652 /* These are for -o noflock - to return ENOSYS on flock calls */
2653 struct file_operations ll_file_operations_noflock = {
2654         .read           = ll_file_read,
2655         .READ_METHOD    = READ_FUNCTION,
2656         .write          = ll_file_write,
2657         .WRITE_METHOD   = WRITE_FUNCTION,
2658         .unlocked_ioctl = ll_file_ioctl,
2659         .open           = ll_file_open,
2660         .release        = ll_file_release,
2661         .mmap           = ll_file_mmap,
2662         .llseek         = ll_file_seek,
2663 #ifdef HAVE_KERNEL_SENDFILE
2664         .sendfile       = ll_file_sendfile,
2665 #endif
2666 #ifdef HAVE_KERNEL_SPLICE_READ
2667         .splice_read    = ll_file_splice_read,
2668 #endif
2669         .fsync          = ll_fsync,
2670         .flush          = ll_flush,
2671         .flock          = ll_file_noflock,
2672         .lock           = ll_file_noflock
2673 };
2674
2675 struct inode_operations ll_file_inode_operations = {
2676         .setattr        = ll_setattr,
2677         .truncate       = ll_truncate,
2678         .getattr        = ll_getattr,
2679         .permission     = ll_inode_permission,
2680         .setxattr       = ll_setxattr,
2681         .getxattr       = ll_getxattr,
2682         .listxattr      = ll_listxattr,
2683         .removexattr    = ll_removexattr,
2684 #ifdef  HAVE_LINUX_FIEMAP_H
2685         .fiemap         = ll_fiemap,
2686 #endif
2687 };
2688
2689 /* dynamic ioctl number support routins */
2690 static struct llioc_ctl_data {
2691         cfs_rw_semaphore_t      ioc_sem;
2692         cfs_list_t              ioc_head;
2693 } llioc = {
2694         __RWSEM_INITIALIZER(llioc.ioc_sem),
2695         CFS_LIST_HEAD_INIT(llioc.ioc_head)
2696 };
2697
2698
2699 struct llioc_data {
2700         cfs_list_t              iocd_list;
2701         unsigned int            iocd_size;
2702         llioc_callback_t        iocd_cb;
2703         unsigned int            iocd_count;
2704         unsigned int            iocd_cmd[0];
2705 };
2706
2707 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
2708 {
2709         unsigned int size;
2710         struct llioc_data *in_data = NULL;
2711         ENTRY;
2712
2713         if (cb == NULL || cmd == NULL ||
2714             count > LLIOC_MAX_CMD || count < 0)
2715                 RETURN(NULL);
2716
2717         size = sizeof(*in_data) + count * sizeof(unsigned int);
2718         OBD_ALLOC(in_data, size);
2719         if (in_data == NULL)
2720                 RETURN(NULL);
2721
2722         memset(in_data, 0, sizeof(*in_data));
2723         in_data->iocd_size = size;
2724         in_data->iocd_cb = cb;
2725         in_data->iocd_count = count;
2726         memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
2727
2728         cfs_down_write(&llioc.ioc_sem);
2729         cfs_list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
2730         cfs_up_write(&llioc.ioc_sem);
2731
2732         RETURN(in_data);
2733 }
2734
2735 void ll_iocontrol_unregister(void *magic)
2736 {
2737         struct llioc_data *tmp;
2738
2739         if (magic == NULL)
2740                 return;
2741
2742         cfs_down_write(&llioc.ioc_sem);
2743         cfs_list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
2744                 if (tmp == magic) {
2745                         unsigned int size = tmp->iocd_size;
2746
2747                         cfs_list_del(&tmp->iocd_list);
2748                         cfs_up_write(&llioc.ioc_sem);
2749
2750                         OBD_FREE(tmp, size);
2751                         return;
2752                 }
2753         }
2754         cfs_up_write(&llioc.ioc_sem);
2755
2756         CWARN("didn't find iocontrol register block with magic: %p\n", magic);
2757 }
2758
2759 EXPORT_SYMBOL(ll_iocontrol_register);
2760 EXPORT_SYMBOL(ll_iocontrol_unregister);
2761
2762 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
2763                         unsigned int cmd, unsigned long arg, int *rcp)
2764 {
2765         enum llioc_iter ret = LLIOC_CONT;
2766         struct llioc_data *data;
2767         int rc = -EINVAL, i;
2768
2769         cfs_down_read(&llioc.ioc_sem);
2770         cfs_list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
2771                 for (i = 0; i < data->iocd_count; i++) {
2772                         if (cmd != data->iocd_cmd[i])
2773                                 continue;
2774
2775                         ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
2776                         break;
2777                 }
2778
2779                 if (ret == LLIOC_STOP)
2780                         break;
2781         }
2782         cfs_up_read(&llioc.ioc_sem);
2783
2784         if (rcp)
2785                 *rcp = rc;
2786         return ret;
2787 }