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