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