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