Whamcloud - gitweb
fixes:
[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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/lustre_dlm.h>
27 #include <linux/lustre_lite.h>
28 #include <linux/pagemap.h>
29 #include <linux/file.h>
30 #include <linux/lustre_acl.h>
31 #include <linux/lustre_sec.h>
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33 #include <linux/lustre_compat25.h>
34 #endif
35 #include <linux/obd_lov.h>
36 #include <linux/lustre_audit.h>
37 #include "llite_internal.h"
38
39 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
40 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
41 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
42
43 int ll_validate_size(struct inode *inode, __u64 *size, __u64 *blocks)
44 {
45         ldlm_policy_data_t extent = { .l_extent = { 0, OBD_OBJECT_EOF } };
46         struct obd_export *exp = ll_i2sbi(inode)->ll_dt_exp;
47         struct ll_inode_info *lli = ll_i2info(inode);
48         struct lustre_handle match_lockh = {0};
49         int rc, flags;
50         ENTRY;
51
52         if (lli->lli_smd == NULL)
53                 RETURN(0);
54
55         LASSERT(size != NULL && blocks != NULL);
56
57         flags = LDLM_FL_TEST_LOCK | LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
58         rc = obd_match(exp, lli->lli_smd, LDLM_EXTENT, &extent,
59                        LCK_PR | LCK_PW, &flags, inode, &match_lockh);
60         if (rc == 0) {
61                 /* we have no all needed locks,
62                  * so we don't know actual size */
63                 GOTO(finish, rc);
64         }
65
66         /* we know actual size! */
67         down(&lli->lli_size_sem);
68         *size = lov_merge_size(lli->lli_smd, 0);
69         *blocks = lov_merge_blocks(lli->lli_smd);
70         up(&lli->lli_size_sem);
71
72 finish:
73         RETURN(rc);
74 }
75
76 int ll_md_och_close(struct obd_export *md_exp, struct inode *inode,
77                     struct obd_client_handle *och, int dirty,
78                     __u64 epoch)
79 {
80         struct ptlrpc_request *req = NULL;
81         struct mdc_op_data *op_data;
82         struct obd_device *obd;
83         obd_valid valid;
84         int rc;
85         ENTRY;
86
87         obd = class_exp2obd(md_exp);
88         if (obd == NULL) {
89                 CERROR("Invalid MDC connection handle "LPX64"\n",
90                        md_exp->exp_handle.h_cookie);
91                 RETURN(0);
92         }
93
94         /* here we check if this is forced umount. If so this is called on
95          * canceling "open lock" and we do not call md_close() in this case , as
96          * it will not successful, as import is already deactivated. */
97         if (obd->obd_no_recov)
98                 GOTO(out, rc = 0);
99
100         /* prepare @op_data for close request */
101         OBD_ALLOC(op_data, sizeof(*op_data));
102         if (!op_data)
103                 RETURN(-ENOMEM);
104
105         memset(op_data, 0, sizeof(*op_data));
106
107         valid = (OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
108                  OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLEPOCH |
109                  OBD_MD_FLID);
110         
111         ll_inode2mdc_data(op_data, inode, valid);
112
113         if (0 /* ll_is_inode_dirty(inode) */) {
114                 op_data->flags = MDS_BFLAG_UNCOMMITTED_WRITES;
115                 op_data->valid |= OBD_MD_FLFLAGS;
116         }
117
118         if (dirty) {
119                 /* we modified data through this handle */
120                 op_data->io_epoch = epoch;
121                 op_data->flags |= MDS_BFLAG_DIRTY_EPOCH;
122                 op_data->valid |= OBD_MD_FLFLAGS | OBD_MD_FLEPOCH;
123                 if (ll_validate_size(inode, &op_data->size, &op_data->blocks))
124                         op_data->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
125         }
126
127         rc = md_close(md_exp, op_data, och, &req);
128         OBD_FREE(op_data, sizeof(*op_data));
129
130         if (rc == EAGAIN) {
131                 /* we are the last writer, so the MDS has instructed us to get
132                  * the file size and any write cookies, then close again. */
133
134                 //ll_queue_done_writing(inode);
135                 rc = 0;
136         } else if (rc) {
137                 CERROR("inode %lu mdc close failed: rc = %d\n",
138                        (unsigned long)inode->i_ino, rc);
139         }
140
141         ptlrpc_req_finished(req);
142         EXIT;
143 out:
144         mdc_clear_open_replay_data(md_exp, och);
145         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
146         OBD_FREE(och, sizeof *och);
147         return rc;
148 }
149
150 int ll_md_real_close(struct obd_export *md_exp,
151                      struct inode *inode, int flags)
152 {
153         struct ll_inode_info *lli = ll_i2info(inode);
154         int freeing = inode->i_state & I_FREEING;
155         struct obd_client_handle **och_p;
156         struct obd_client_handle *och;
157         __u64 *och_usecount, epoch;
158         int rc = 0, dirty = 0;
159         ENTRY;
160
161         if (flags & FMODE_WRITE) {
162                 och_p = &lli->lli_mds_write_och;
163                 och_usecount = &lli->lli_open_fd_write_count;
164         } else if (flags & FMODE_EXEC) {
165                 och_p = &lli->lli_mds_exec_och;
166                 och_usecount = &lli->lli_open_fd_exec_count;
167          } else {
168                 och_p = &lli->lli_mds_read_och;
169                 och_usecount = &lli->lli_open_fd_read_count;
170         }
171
172         down(&lli->lli_och_sem);
173         if (*och_usecount) { /* There are still users of this handle, so
174                                 skip freeing it. */
175                 up(&lli->lli_och_sem);
176                 RETURN(0);
177         }
178         if (ll_is_inode_dirty(inode)) {
179                 /* the inode still has dirty pages, let's close later */
180                 CDEBUG(D_INODE, "inode %lu/%u still has dirty pages\n",
181                        inode->i_ino, inode->i_generation);
182                 LASSERT(freeing == 0);
183                 ll_queue_done_writing(inode);
184                 up(&lli->lli_och_sem);
185                 RETURN(0);
186         }
187         
188         if (LLI_DIRTY_HANDLE(inode) && (flags & FMODE_WRITE)) {
189                 clear_bit(LLI_F_DIRTY_HANDLE,  &lli->lli_flags);
190                 dirty = 1;
191         } else if (0 && !(flags & FMODE_SYNC) && !freeing) {
192                 /* in order to speed up creation rate we pass
193                  * closing to dedicated thread so we don't need
194                  * to wait for close reply here -bzzz */
195                 ll_queue_done_writing(inode);
196                 up(&lli->lli_och_sem);
197                 RETURN(0);
198         }
199
200         och = *och_p;
201         *och_p = NULL;
202         epoch = lli->lli_io_epoch;
203         lli->lli_io_epoch = 0;
204
205         up(&lli->lli_och_sem);
206
207         /*
208          * there might be a race and somebody have freed this och
209          * already. Another way to have this twice called is if file closing
210          * will fail due to netwok problems and on umount lock will be canceled
211          * and this will be called from block_ast callack.
212         */
213         if (och && och->och_fh.cookie != DEAD_HANDLE_MAGIC)
214                 rc = ll_md_och_close(md_exp, inode, och, dirty, epoch);
215         
216         RETURN(rc);
217 }
218
219 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
220                 struct file *file)
221 {
222         struct ll_file_data *fd = file->private_data;
223         struct ll_inode_info *lli = ll_i2info(inode);
224         int rc = 0;
225         ENTRY;
226
227         /* clear group lock, if present */
228         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
229                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
230                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
231                 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
232                                       &fd->fd_cwlockh);
233         }
234
235         /* Let's see if we have good enough OPEN lock on the file and if
236            we can skip talking to MDS */
237         if (file->f_dentry->d_inode) {
238                 int lockmode;
239                 struct obd_device *obddev;
240                 struct lustre_handle lockh;
241                 int flags = LDLM_FL_BLOCK_GRANTED;
242                 struct ldlm_res_id file_res_id = {.name = {id_fid(&lli->lli_id), 
243                                                            id_group(&lli->lli_id)}};
244                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
245
246                 down(&lli->lli_och_sem);
247                 if (fd->fd_omode & FMODE_WRITE) {
248                         lockmode = LCK_CW;
249                         LASSERT(lli->lli_open_fd_write_count);
250                         lli->lli_open_fd_write_count--;
251                 } else if (fd->fd_omode & FMODE_EXEC) {
252                         lockmode = LCK_PR;
253                         LASSERT(lli->lli_open_fd_exec_count);
254                         lli->lli_open_fd_exec_count--;
255                 } else {
256                         lockmode = LCK_CR;
257                         LASSERT(lli->lli_open_fd_read_count);
258                         lli->lli_open_fd_read_count--;
259                 }
260                 up(&lli->lli_och_sem);
261                 
262                 obddev = md_get_real_obd(md_exp, &lli->lli_id);
263                 if (!ldlm_lock_match(obddev->obd_namespace, flags, &file_res_id,
264                                      LDLM_IBITS, &policy, lockmode, &lockh))
265                 {
266                         rc = ll_md_real_close(md_exp, file->f_dentry->d_inode,
267                                               fd->fd_omode);
268                 } else {
269                         ldlm_lock_decref(&lockh, lockmode);
270                 }
271         }
272
273         file->private_data = NULL;
274         OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof(*fd));
275         RETURN(rc);
276 }
277
278 /* While this returns an error code, fput() the caller does not, so we need
279  * to make every effort to clean up all of our state here.  Also, applications
280  * rarely check close errors and even if an error is returned they will not
281  * re-try the close call.
282  */
283 int ll_file_release(struct inode *inode, struct file *file)
284 {
285         struct ll_file_data *fd;
286         struct ll_sb_info *sbi = ll_i2sbi(inode);
287         int rc;
288
289         ENTRY;
290         CDEBUG(D_VFSTRACE, "VFS Op:inode="DLID4"(%p)\n",
291                OLID4(&ll_i2info(inode)->lli_id), inode);
292
293         /* don't do anything for / */
294         if (inode->i_sb->s_root == file->f_dentry)
295                 RETURN(0);
296
297         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
298         fd = (struct ll_file_data *)file->private_data;
299         LASSERT(fd != NULL);
300
301         rc = ll_md_close(sbi->ll_md_exp, inode, file);
302         RETURN(rc);
303 }
304
305 static int ll_intent_file_open(struct file *file, void *lmm,
306                                int lmmsize, struct lookup_intent *itp)
307 {
308         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
309         const char *name = (char *)file->f_dentry->d_name.name;
310         struct dentry *parent = file->f_dentry->d_parent;
311         const int len = file->f_dentry->d_name.len;
312         struct lustre_handle lockh;
313         struct mdc_op_data *op_data;
314         int rc;
315
316         if (!parent)
317                 RETURN(-ENOENT);
318
319         OBD_ALLOC(op_data, sizeof(*op_data));
320         if (op_data == NULL)
321                 RETURN(-ENOMEM);
322         
323         ll_prepare_mdc_data(op_data, parent->d_inode, NULL,
324                             name, len, O_RDWR);
325
326         rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, itp, LCK_PR, op_data,
327                         &lockh, lmm, lmmsize, ldlm_completion_ast,
328                         ll_mdc_blocking_ast, NULL);
329         OBD_FREE(op_data, sizeof(*op_data));
330         if (rc == 0) {
331                 if (LUSTRE_IT(itp)->it_lock_mode)
332                         memcpy(&LUSTRE_IT(itp)->it_lock_handle,
333                                &lockh, sizeof(lockh));
334
335         } else if (rc < 0) {
336                 CERROR("lock enqueue: err: %d\n", rc);
337         }
338         RETURN(rc);
339 }
340
341 int ll_och_fill(struct inode *inode, struct lookup_intent *it,
342                  struct obd_client_handle *och)
343 {
344         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
345         struct ll_inode_info *lli = ll_i2info(inode);
346         struct mds_body *body;
347         int rc = 0;
348         ENTRY;
349         LASSERT(och);
350
351         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
352         LASSERT (body != NULL);          /* reply already checked out */
353         LASSERT_REPSWABBED (req, 1);     /* and swabbed down */
354
355         memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
356         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
357         if (lli->lli_io_epoch != 0 && lli->lli_io_epoch != body->io_epoch)
358                 CDEBUG(D_ERROR, "are we opening new epoch?! "LPD64
359                        " != "LPD64"\n", lli->lli_io_epoch, body->io_epoch);
360         lli->lli_io_epoch = body->io_epoch;
361         mdc_set_open_replay_data(ll_i2mdexp(inode), och, 
362                                  LUSTRE_IT(it)->it_data);
363
364         if (S_ISREG(inode->i_mode) && (body->valid & OBD_MD_CAPA))
365                 rc = ll_set_capa(inode, it);
366         RETURN(rc);
367 }
368
369 int ll_local_open(struct file *file, struct lookup_intent *it,
370                   struct obd_client_handle *och)
371 {
372         struct ll_file_data *fd;
373         int rc = 0;
374         ENTRY;
375
376         if (och) {
377                 rc = ll_och_fill(file->f_dentry->d_inode, it, och);
378                 if (rc)
379                         RETURN(rc);
380         }
381
382         LASSERTF(file->private_data == NULL, "file %.*s/%.*s ino %lu/%u (%o)\n",
383                  file->f_dentry->d_name.len, file->f_dentry->d_name.name,
384                  file->f_dentry->d_parent->d_name.len,
385                  file->f_dentry->d_parent->d_name.name,
386                  file->f_dentry->d_inode->i_ino,
387                  file->f_dentry->d_inode->i_generation,
388                  file->f_dentry->d_inode->i_mode);
389
390         OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
391         
392         /* We can't handle this well without reorganizing ll_file_open and
393          * ll_md_close(), so don't even try right now. */
394         LASSERT(fd != NULL);
395
396         file->private_data = fd;
397         ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
398         fd->fd_omode = it->it_flags;
399
400         RETURN(0);
401 }
402
403 /* Open a file, and (for the very first open) create objects on the OSTs at
404  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
405  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
406  * lli_open_sem to ensure no other process will create objects, send the
407  * stripe MD to the MDS, or try to destroy the objects if that fails.
408  *
409  * If we already have the stripe MD locally then we don't request it in
410  * mdc_open(), by passing a lmm_size = 0.
411  *
412  * It is up to the application to ensure no other processes open this file
413  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
414  * used.  We might be able to avoid races of that sort by getting lli_open_sem
415  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
416  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
417  */
418 int ll_file_open(struct inode *inode, struct file *file)
419 {
420         struct ll_inode_info *lli = ll_i2info(inode);
421         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
422                                           .it_flags = file->f_flags };
423         struct lov_stripe_md *lsm;
424         struct ptlrpc_request *req;
425         int rc = 0;
426         struct obd_client_handle **och_p = NULL;
427         __u64 *och_usecount = NULL;
428         ENTRY;
429
430         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n",
431                inode->i_ino, inode->i_generation, inode, file->f_flags);
432
433         /* don't do anything for / */
434         if (inode->i_sb->s_root == file->f_dentry)
435                 RETURN(0);
436
437         if ((file->f_flags+1) & O_ACCMODE)
438                 oit.it_flags++;
439         if (file->f_flags & O_TRUNC)
440                 oit.it_flags |= 2;
441
442         it = file->f_it;
443         /*
444          * sometimes LUSTRE_IT(it) may not be allocated like opening file by
445          * dentry_open() from GNS stuff.
446          */
447         if (!it || !LUSTRE_IT(it)) {
448                 it = &oit;
449                 rc = ll_intent_alloc(it);
450                 if (rc)
451                         GOTO(out, rc);
452         }
453
454         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
455         
456         /*
457          * mdc_intent_lock() didn't get a request ref if there was an open
458          * error, so don't do cleanup on the * request here (bug 3430)
459          */
460         if (LUSTRE_IT(it)->it_disposition) {
461                 rc = it_open_error(DISP_OPEN_OPEN, it);
462                 if (rc)
463                         RETURN(rc);
464         }
465
466         /* Let's see if we have file open on MDS already. */
467         if (it->it_flags & FMODE_WRITE) {
468                 och_p = &lli->lli_mds_write_och;
469                 och_usecount = &lli->lli_open_fd_write_count;
470         } else if (it->it_flags & FMODE_EXEC) {
471                 och_p = &lli->lli_mds_exec_och;
472                 och_usecount = &lli->lli_open_fd_exec_count;
473         } else {
474                 och_p = &lli->lli_mds_read_och;
475                 och_usecount = &lli->lli_open_fd_read_count;
476         }
477
478         rc = ll_crypto_decrypt_key(inode, it);
479         if (rc)
480                 GOTO(out, rc);
481
482         down(&lli->lli_och_sem);
483         if (*och_p) { /* Open handle is present */
484                 if (it_disposition(it, DISP_LOOKUP_POS) && /* Positive lookup */
485                     it_disposition(it, DISP_OPEN_OPEN)) { /* & OPEN happened */
486                         struct obd_client_handle *och;
487                         /* Well, there's extra open request that we do not need,
488                            let's close it somehow*/
489                         OBD_ALLOC(och, sizeof (struct obd_client_handle));
490                         if (!och) {
491                                 up(&lli->lli_och_sem);
492                                 RETURN(-ENOMEM);
493                         }
494
495                         rc = ll_och_fill(inode, it, och);
496                         if (rc) {
497                                 up(&lli->lli_och_sem);
498                                 RETURN(rc);
499                         }
500                         
501                         /* ll_md_och_close() will free och */
502                         ll_md_och_close(ll_i2mdexp(inode), inode, och, 0, 0);
503                 }
504                 (*och_usecount)++;
505                         
506                 rc = ll_local_open(file, it, NULL);
507                 if (rc)
508                         LBUG();
509         } else {
510                 LASSERT(*och_usecount == 0);
511                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
512                 if (!*och_p)
513                         GOTO(out, rc = -ENOMEM);
514                 (*och_usecount)++;
515
516                 if (!it || !LUSTRE_IT(it) || !LUSTRE_IT(it)->it_disposition) {
517                         /*
518                          * we are going to replace intent here, and that may
519                          * possibly change access mode (FMODE_EXEC can only be
520                          * set in intent), but I hope it never happens (I was
521                          * not able to trigger it yet at least) -- green
522                          */
523                         
524                         /* FIXME: FMODE_EXEC is not covered by O_ACCMODE! */
525                         LASSERT(!(it->it_flags & FMODE_EXEC));
526                         LASSERTF((it->it_flags & O_ACCMODE) ==
527                                  (oit.it_flags & O_ACCMODE), "Changing intent "
528                                  "flags %x to incompatible %x\n", it->it_flags,
529                                  oit.it_flags);
530                         it = &oit;
531                         rc = ll_intent_file_open(file, NULL, 0, it);
532                         if (rc)
533                                 GOTO(out, rc);
534                         rc = it_open_error(DISP_OPEN_OPEN, it);
535                         if (rc)
536                                 GOTO(out_och_free, rc);
537
538                         mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle,
539                                           file->f_dentry->d_inode);
540                 }
541                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
542                 rc = ll_local_open(file, it, *och_p);
543                 LASSERTF(rc == 0, "rc = %d\n", rc);
544         }
545         up(&lli->lli_och_sem);
546         
547         /*
548          * must do this outside lli_och_sem lock to prevent deadlock where
549          * different kind of OPEN lock for this same inode gets cancelled by
550          * ldlm_cancel_lru
551          */
552         if (!S_ISREG(inode->i_mode))
553                 GOTO(out, rc);
554
555         lsm = lli->lli_smd;
556         if (lsm == NULL) {
557                 if (file->f_flags & O_LOV_DELAY_CREATE ||
558                     !(file->f_mode & FMODE_WRITE)) {
559                         CDEBUG(D_INODE, "object creation was delayed\n");
560                         GOTO(out, rc);
561                 }
562         }
563         file->f_flags &= ~O_LOV_DELAY_CREATE;
564         GOTO(out, rc);
565  out:
566         /* audit stuff if there was no RPC */
567         if (LUSTRE_IT(it)->it_data == 0) {
568                 ll_audit_log(inode, AUDIT_OPEN, rc);
569         }
570
571         req = LUSTRE_IT(it)->it_data;
572         ll_intent_drop_lock(it);
573         ll_intent_release(it);
574         ptlrpc_req_finished(req);
575         if (rc == 0) {
576                 ll_open_complete(inode);
577         } else {
578 out_och_free:
579                 if (*och_p) {
580                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
581                         *och_p = NULL; /* OBD_FREE writes some magic there */
582                         (*och_usecount)--;
583                 }
584                 up(&lli->lli_och_sem);
585         }
586                 
587         return rc;
588 }
589
590 /* Fills the obdo with the attributes for the inode defined by lsm */
591 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
592                    struct obdo *oa)
593 {
594         struct ptlrpc_request_set *set;
595         int rc;
596         ENTRY;
597
598         LASSERT(lsm != NULL);
599
600         memset(oa, 0, sizeof *oa);
601         oa->o_id = lsm->lsm_object_id;
602         oa->o_gr = lsm->lsm_object_gr;
603         oa->o_mode = S_IFREG;
604         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
605                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
606                 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
607
608         set = ptlrpc_prep_set();
609         if (set == NULL) {
610                 rc = -ENOMEM;
611         } else {
612                 rc = obd_getattr_async(exp, oa, lsm, set);
613                 if (rc == 0)
614                         rc = ptlrpc_set_wait(set);
615                 ptlrpc_set_destroy(set);
616         }
617         if (rc)
618                 RETURN(rc);
619
620         oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
621                         OBD_MD_FLCTIME | OBD_MD_FLSIZE);
622         RETURN(0);
623 }
624
625 static inline void ll_remove_suid(struct inode *inode)
626 {
627         unsigned int mode;
628
629         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
630         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
631
632         /* was any of the uid bits set? */
633         mode &= inode->i_mode;
634         if (mode && !capable(CAP_FSETID)) {
635                 inode->i_mode &= ~mode;
636                 // XXX careful here - we cannot change the size
637         }
638 }
639
640 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
641 {
642         struct ll_inode_info *lli = ll_i2info(inode);
643         struct lov_stripe_md *lsm = lli->lli_smd;
644         struct obd_export *exp = ll_i2dtexp(inode);
645         struct {
646                 char name[16];
647                 struct ldlm_lock *lock;
648                 struct lov_stripe_md *lsm;
649         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
650         __u32 stripe, vallen = sizeof(stripe);
651         int rc;
652         ENTRY;
653
654         if (lsm->lsm_stripe_count == 1)
655                 GOTO(check, stripe = 0);
656
657         /* get our offset in the lov */
658         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
659         if (rc != 0) {
660                 CERROR("obd_get_info: rc = %d\n", rc);
661                 RETURN(rc);
662         }
663         LASSERT(stripe < lsm->lsm_stripe_count);
664         EXIT;
665 check:
666         if (lsm->lsm_oinfo[stripe].loi_id != lock->l_resource->lr_name.name[0]||
667             lsm->lsm_oinfo[stripe].loi_gr != lock->l_resource->lr_name.name[2]){
668                 LDLM_ERROR(lock, "resource doesn't match object "LPU64"/"LPU64
669                            " inode=%lu/%u (%p)\n",
670                            lsm->lsm_oinfo[stripe].loi_id,
671                            lsm->lsm_oinfo[stripe].loi_gr,
672                            inode->i_ino, inode->i_generation, inode);
673                 return -ELDLM_NO_LOCK_DATA;
674         }
675
676         return stripe;
677 }
678
679 /* Flush the page cache for an extent as its canceled.  When we're on an LOV,
680  * we get a lock cancellation for each stripe, so we have to map the obd's
681  * region back onto the stripes in the file that it held.
682  *
683  * No one can dirty the extent until we've finished our work and they can
684  * enqueue another lock.  The DLM protects us from ll_file_read/write here,
685  * but other kernel actors could have pages locked.
686  *
687  * Called with the DLM lock held. */
688 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
689                               struct ldlm_lock *lock, __u32 stripe)
690 {
691         ldlm_policy_data_t tmpex;
692         unsigned long start, end, count, skip, i, j;
693         struct page *page;
694         int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
695         struct lustre_handle lockh;
696         ENTRY;
697
698         memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
699         CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
700                inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
701                inode->i_size);
702
703         /* our locks are page granular thanks to osc_enqueue, we invalidate the
704          * whole page. */
705         LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
706         LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
707
708         count = ~0;
709         skip = 0;
710         start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
711         end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
712         if (lsm->lsm_stripe_count > 1) {
713                 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
714                 skip = (lsm->lsm_stripe_count - 1) * count;
715                 start += start/count * skip + stripe * count;
716                 if (end != ~0)
717                         end += end/count * skip + stripe * count;
718         }
719         if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
720                 end = ~0;
721
722         i = inode->i_size ? (inode->i_size - 1) >> PAGE_CACHE_SHIFT : 0;
723         if (i < end)
724                 end = i;
725
726         CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
727                "count: %lu skip: %lu end: %lu%s\n", start, start % count,
728                count, skip, end, discard ? " (DISCARDING)" : "");
729         
730         /* walk through the vmas on the inode and tear down mmaped pages that
731          * intersect with the lock.  this stops immediately if there are no
732          * mmap()ed regions of the file.  This is not efficient at all and
733          * should be short lived. We'll associate mmap()ed pages with the lock
734          * and will be able to find them directly */
735         
736         for (i = start; i <= end; i += (j + skip)) {
737                 j = min(count - (i % count), end - i + 1);
738                 LASSERT(j > 0);
739                 LASSERT(inode->i_mapping);
740                 if (ll_teardown_mmaps(inode->i_mapping, 
741                                       (__u64)i << PAGE_CACHE_SHIFT,
742                                       ((__u64)(i+j) << PAGE_CACHE_SHIFT) - 1) )
743                         break;
744         }
745
746         /* this is the simplistic implementation of page eviction at
747          * cancelation.  It is careful to get races with other page
748          * lockers handled correctly.  fixes from bug 20 will make it
749          * more efficient by associating locks with pages and with
750          * batching writeback under the lock explicitly. */
751         for (i = start, j = start % count; i <= end;
752              j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
753                 if (j == count) {
754                         CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
755                         i += skip;
756                         j = 0;
757                         if (i > end)
758                                 break;
759                 }
760                 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
761                          LPU64" >= "LPU64" start %lu i %lu end %lu\n",
762                          tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
763                          start, i, end);
764
765                 if (!mapping_has_pages(inode->i_mapping)) {
766                         CDEBUG(D_INODE|D_PAGE, "nothing left\n");
767                         break;
768                 }
769
770                 cond_resched();
771
772                 page = find_get_page(inode->i_mapping, i);
773                 if (page == NULL)
774                         continue;
775                 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
776                                i, tmpex.l_extent.start);
777                 lock_page(page);
778
779                 /* page->mapping to check with racing against teardown */
780                 if (!discard && clear_page_dirty_for_io(page)) {
781                         rc = ll_call_writepage(inode, page);
782                         if (rc != 0)
783                                 CERROR("writepage of page %p failed: %d\n",
784                                        page, rc);
785                         /* either waiting for io to complete or reacquiring
786                          * the lock that the failed writepage released */
787                         lock_page(page);
788                 }
789
790                 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
791                 /* check to see if another DLM lock covers this page */
792                 rc2 = ldlm_lock_match(lock->l_resource->lr_namespace,
793                                       LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
794                                       LDLM_FL_TEST_LOCK,
795                                       &lock->l_resource->lr_name, LDLM_EXTENT,
796                                       &tmpex, LCK_PR | LCK_PW, &lockh);
797                 if (rc2 == 0 && page->mapping != NULL) {
798                         // checking again to account for writeback's lock_page()
799                         LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
800                         ll_ra_accounting(page, inode->i_mapping);
801                         ll_truncate_complete_page(page);
802                 }
803                 unlock_page(page);
804                 page_cache_release(page);
805         }
806         LASSERTF(tmpex.l_extent.start <=
807                  (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
808                   lock->l_policy_data.l_extent.end + 1),
809                  "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
810                  tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
811                  start, i, end);
812         EXIT;
813 }
814
815 static int ll_extent_lock_callback(struct ldlm_lock *lock,
816                                    struct ldlm_lock_desc *new, void *data,
817                                    int flag)
818 {
819         struct lustre_handle lockh = { 0 };
820         int rc;
821         ENTRY;
822
823         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
824                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
825                 LBUG();
826         }
827
828         switch (flag) {
829         case LDLM_CB_BLOCKING:
830                 ldlm_lock2handle(lock, &lockh);
831                 rc = ldlm_cli_cancel(&lockh);
832                 if (rc != ELDLM_OK)
833                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
834                 break;
835         case LDLM_CB_CANCELING: {
836                 struct inode *inode;
837                 struct ll_inode_info *lli;
838                 struct lov_stripe_md *lsm;
839                 __u32 stripe;
840                 __u64 kms;
841
842                 /* This lock wasn't granted, don't try to evict pages */
843                 if (lock->l_req_mode != lock->l_granted_mode)
844                         RETURN(0);
845
846                 inode = ll_inode_from_lock(lock);
847                 if (inode == NULL)
848                         RETURN(0);
849                 lli = ll_i2info(inode);
850                 if (lli == NULL)
851                         goto iput;
852                 if (lli->lli_smd == NULL)
853                         goto iput;
854                 lsm = lli->lli_smd;
855
856                 stripe = ll_lock_to_stripe_offset(inode, lock);
857                 if (stripe < 0) {
858                         CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
859                         goto iput;
860                 }
861
862                 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
863
864                 down(&lli->lli_size_sem);
865                 lock_res_and_lock(lock);
866                 kms = ldlm_extent_shift_kms(lock,
867                                             lsm->lsm_oinfo[stripe].loi_kms);
868                 
869                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
870                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
871                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
872                 lsm->lsm_oinfo[stripe].loi_kms = kms;
873                 unlock_res_and_lock(lock);
874                 up(&lli->lli_size_sem);
875                 //ll_try_done_writing(inode);
876         iput:
877                 iput(inode);
878                 break;
879         }
880         default:
881                 LBUG();
882         }
883
884         RETURN(0);
885 }
886
887 #if 0
888 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
889 {
890         /* XXX ALLOCATE - 160 bytes */
891         struct inode *inode = ll_inode_from_lock(lock);
892         struct ll_inode_info *lli = ll_i2info(inode);
893         struct lustre_handle lockh = { 0 };
894         struct ost_lvb *lvb;
895         __u32 stripe;
896         ENTRY;
897
898         if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
899                      LDLM_FL_BLOCK_CONV)) {
900                 LBUG(); /* not expecting any blocked async locks yet */
901                 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
902                            "lock, returning");
903                 ldlm_lock_dump(D_OTHER, lock, 0);
904                 ldlm_reprocess_all(lock->l_resource);
905                 RETURN(0);
906         }
907
908         LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
909
910         stripe = ll_lock_to_stripe_offset(inode, lock);
911         if (stripe < 0) {
912                 CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
913                 goto iput;
914         }
915
916         if (lock->l_lvb_len) {
917                 struct lov_stripe_md *lsm = lli->lli_smd;
918                 __u64 kms;
919                 lvb = lock->l_lvb_data;
920                 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
921
922                 down(&inode->i_sem);
923                 lock_res_and_lock(lock);
924                 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
925                 kms = ldlm_extent_shift_kms(NULL, kms);
926                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
927                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
928                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
929                 lsm->lsm_oinfo[stripe].loi_kms = kms;
930                 unlock_res_and_lock(lock);
931                 up(&inode->i_sem);
932         }
933
934 iput:
935         iput(inode);
936         wake_up(&lock->l_waitq);
937
938         ldlm_lock2handle(lock, &lockh);
939         ldlm_lock_decref(&lockh, LCK_PR);
940         RETURN(0);
941 }
942 #endif
943
944 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
945 {
946         struct ptlrpc_request *req = reqp;
947         struct inode *inode = ll_inode_from_lock(lock);
948         struct ll_inode_info *lli;
949         struct ost_lvb *lvb;
950         struct lov_stripe_md *lsm;
951         int rc, size = sizeof(*lvb), stripe;
952         ENTRY;
953
954         if (inode == NULL)
955                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
956         lli = ll_i2info(inode);
957         if (lli == NULL)
958                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
959
960         lsm = lli->lli_smd;
961         if (lsm == NULL)
962                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
963
964         /* First, find out which stripe index this lock corresponds to. */
965         stripe = ll_lock_to_stripe_offset(inode, lock);
966         if (stripe < 0) {
967                 CERROR("ll_lock_to_stripe_offset failed: %d\n", stripe);
968                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
969         }
970
971         rc = lustre_pack_reply(req, 1, &size, NULL);
972         if (rc) {
973                 CERROR("lustre_pack_reply: %d\n", rc);
974                 GOTO(iput, rc);
975         }
976
977         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
978         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
979         lvb->lvb_mtime = LTIME_S(inode->i_mtime);
980         lvb->lvb_atime = LTIME_S(inode->i_atime);
981         lvb->lvb_ctime = LTIME_S(inode->i_ctime);
982
983         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
984                    inode->i_size, stripe, lvb->lvb_size);
985         GOTO(iput, 0);
986  iput:
987         iput(inode);
988
989  out:
990         /* These errors are normal races, so we don't want to fill the console
991          * with messages by calling ptlrpc_error() */
992         if (rc == -ELDLM_NO_LOCK_DATA)
993                 lustre_pack_reply(req, 0, NULL, NULL);
994
995         req->rq_status = rc;
996         return rc;
997 }
998
999 /* NB: lov_merge_size will prefer locally cached writes if they extend the
1000  * file (because it prefers KMS over RSS when larger) */
1001 int ll_glimpse_size(struct inode *inode)
1002 {
1003         struct ll_inode_info *lli = ll_i2info(inode);
1004         struct ll_sb_info *sbi = ll_i2sbi(inode);
1005         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1006         struct lustre_handle lockh = { 0 };
1007         int rc, flags = LDLM_FL_HAS_INTENT;
1008         ENTRY;
1009
1010         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
1011
1012         rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
1013                          LCK_PR, &flags, ll_extent_lock_callback,
1014                          ldlm_completion_ast, ll_glimpse_callback, inode,
1015                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
1016         if (rc == -ENOENT)
1017                 RETURN(rc);
1018
1019         if (rc != 0) {
1020                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
1021                 RETURN(rc > 0 ? -EIO : rc);
1022         }
1023
1024         down(&lli->lli_size_sem);
1025         inode->i_size = lov_merge_size(lli->lli_smd, 0);
1026         inode->i_blocks = lov_merge_blocks(lli->lli_smd);
1027         up(&lli->lli_size_sem);
1028
1029         LTIME_S(inode->i_mtime) = lov_merge_mtime(lli->lli_smd,
1030                                                   LTIME_S(inode->i_mtime));
1031
1032         CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
1033                (__u64)inode->i_size, (__u64)inode->i_blocks);
1034         
1035         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
1036         RETURN(rc);
1037 }
1038
1039 void ll_stime_record(struct ll_sb_info *sbi, struct timeval *start,
1040                     struct obd_service_time *stime)
1041 {
1042         struct timeval stop;
1043         do_gettimeofday(&stop);
1044                                                                                                                                                                                                      
1045         spin_lock(&sbi->ll_lock);
1046         lprocfs_stime_record(stime, &stop, start);
1047         spin_unlock(&sbi->ll_lock);
1048 }
1049
1050 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
1051                    struct lov_stripe_md *lsm, int mode,
1052                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
1053                    int ast_flags, struct obd_service_time *stime)
1054 {
1055         struct ll_inode_info *lli = ll_i2info(inode);
1056         struct ll_sb_info *sbi = ll_i2sbi(inode);
1057         struct timeval start;
1058         int rc;
1059         ENTRY;
1060
1061         LASSERT(lockh->cookie == 0);
1062
1063         /* XXX phil: can we do this?  won't it screw the file size up? */
1064         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
1065             (sbi->ll_flags & LL_SBI_NOLCK))
1066                 RETURN(0);
1067
1068         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
1069                inode->i_ino, policy->l_extent.start, policy->l_extent.end);
1070
1071         do_gettimeofday(&start);        
1072         rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
1073                          &ast_flags, ll_extent_lock_callback,
1074                          ldlm_completion_ast, ll_glimpse_callback, inode,
1075                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
1076         if (rc > 0)
1077                 rc = -EIO;
1078         
1079         ll_stime_record(sbi, &start, stime);
1080
1081         if (policy->l_extent.start == 0 &&
1082             policy->l_extent.end == OBD_OBJECT_EOF) {
1083                 /* vmtruncate()->ll_truncate() first sets the i_size and then
1084                  * the kms under both a DLM lock and the i_sem.  If we don't
1085                  * get the i_sem here we can match the DLM lock and reset
1086                  * i_size from the kms before the truncating path has updated
1087                  * the kms.  generic_file_write can then trust the stale i_size
1088                  * when doing appending writes and effectively cancel the
1089                  * result of the truncate.  Getting the i_sem after the enqueue
1090                  * maintains the DLM -> i_sem acquiry order. */
1091                 down(&lli->lli_size_sem);
1092                 inode->i_size = lov_merge_size(lsm, 1);
1093                 up(&lli->lli_size_sem);
1094         }
1095         
1096         if (rc == 0) {
1097                 LTIME_S(inode->i_mtime) =
1098                         lov_merge_mtime(lsm, LTIME_S(inode->i_mtime));
1099         }
1100
1101         RETURN(rc);
1102 }
1103
1104 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
1105                      struct lov_stripe_md *lsm, int mode,
1106                      struct lustre_handle *lockh)
1107 {
1108         struct ll_sb_info *sbi = ll_i2sbi(inode);
1109         int rc;
1110         ENTRY;
1111
1112         /* XXX phil: can we do this?  won't it screw the file size up? */
1113         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
1114             (sbi->ll_flags & LL_SBI_NOLCK))
1115                 RETURN(0);
1116
1117         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
1118
1119         RETURN(rc);
1120 }
1121
1122 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1123                             loff_t *ppos)
1124 {
1125         struct inode *inode = file->f_dentry->d_inode;
1126         struct ll_inode_info *lli = ll_i2info(inode);
1127         struct lov_stripe_md *lsm = lli->lli_smd;
1128         struct ll_lock_tree tree;
1129         struct ll_lock_tree_node *node;
1130         int rc;
1131         ssize_t retval;
1132         __u64 kms;
1133         ENTRY;
1134         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1135                inode->i_ino, inode->i_generation, inode, count, *ppos);
1136
1137         /* "If nbyte is 0, read() will return 0 and have no other results."
1138          *                      -- Single Unix Spec */
1139         if (count == 0)
1140                 RETURN(0);
1141
1142         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
1143                             count);
1144
1145         if (!lsm)
1146                 RETURN(0);
1147
1148         node = ll_node_from_inode(inode, *ppos, *ppos  + count - 1,
1149                                   LCK_PR);
1150
1151         tree.lt_fd = file->private_data;
1152         
1153         rc = ll_tree_lock(&tree, node, inode, buf, count,
1154                           file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1155         if (rc != 0)
1156                 RETURN(rc);
1157
1158         down(&lli->lli_size_sem);
1159         kms = lov_merge_size(lsm, 1);
1160         if (*ppos + count - 1 > kms) {
1161                 /* A glimpse is necessary to determine whether we return a short
1162                  * read or some zeroes at the end of the buffer */
1163                 up(&lli->lli_size_sem);
1164                 retval = ll_glimpse_size(inode);
1165                 if (retval)
1166                         goto out;
1167         } else {
1168                 inode->i_size = kms;
1169                 up(&lli->lli_size_sem);
1170         }
1171
1172         CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
1173                inode->i_ino, count, *ppos, inode->i_size);
1174         
1175         /* turn off the kernel's read-ahead */
1176 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1177         file->f_ramax = 0;
1178 #else
1179         file->f_ra.ra_pages = 0;
1180 #endif
1181         retval = generic_file_read(file, buf, count, ppos);
1182         ll_audit_log(inode, AUDIT_READ, retval);
1183
1184  out:
1185         ll_tree_unlock(&tree, inode);
1186         RETURN(retval);
1187 }
1188
1189 /*
1190  * Write to a file (through the page cache).
1191  */
1192 static ssize_t ll_file_write(struct file *file, const char *buf,
1193                              size_t count, loff_t *ppos)
1194 {
1195         struct inode *inode = file->f_dentry->d_inode;
1196         loff_t maxbytes = ll_file_maxbytes(inode);
1197         struct ll_lock_tree tree;
1198         struct ll_lock_tree_node *node;
1199         ssize_t retval;
1200         int rc;
1201
1202         ENTRY;
1203         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1204                inode->i_ino, inode->i_generation, inode, count, *ppos);
1205
1206         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1207
1208         /* POSIX, but surprised the VFS doesn't check this already */
1209         if (count == 0)
1210                 RETURN(0);
1211
1212         /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
1213          * called on the file, don't fail the below assertion (bug 2388). */
1214         if (file->f_flags & O_LOV_DELAY_CREATE &&
1215             ll_i2info(inode)->lli_smd == NULL)
1216                 RETURN(-EBADF);
1217
1218         LASSERT(ll_i2info(inode)->lli_smd != NULL);
1219         
1220         if (file->f_flags & O_APPEND)
1221                 node = ll_node_from_inode(inode, 0, OBD_OBJECT_EOF, LCK_PW);
1222         else
1223                 node = ll_node_from_inode(inode, *ppos, *ppos  + count - 1,
1224                                           LCK_PW);
1225
1226         if (IS_ERR(node))
1227                 RETURN(PTR_ERR(node));
1228
1229         tree.lt_fd = file->private_data;
1230         
1231         rc = ll_tree_lock(&tree, node, inode, buf, count,
1232                           file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1233         if (rc != 0)
1234                 RETURN(rc);
1235
1236         /* this is ok, g_f_w will overwrite this under i_sem if it races
1237          * with a local truncate, it just makes our maxbyte checking easier */
1238         if (file->f_flags & O_APPEND)
1239                 *ppos = inode->i_size;
1240
1241         if (*ppos >= maxbytes) {
1242                 if (count || *ppos > maxbytes) {
1243                         send_sig(SIGXFSZ, current, 0);
1244                         GOTO(out, retval = -EFBIG);
1245                 }
1246         }
1247         if (*ppos + count > maxbytes)
1248                 count = maxbytes - *ppos;
1249
1250         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
1251                inode->i_ino, count, *ppos);
1252
1253         /* mark open handle dirty */
1254         set_bit(LLI_F_DIRTY_HANDLE, &(ll_i2info(inode)->lli_flags));
1255
1256         /* generic_file_write handles O_APPEND after getting i_sem */
1257         retval = generic_file_write(file, buf, count, ppos);
1258
1259         EXIT;
1260 out:
1261         ll_audit_log(inode, AUDIT_WRITE, retval);
1262
1263         ll_tree_unlock(&tree, inode);
1264         /* serialize with mmap/munmap/mremap */
1265         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
1266                             retval > 0 ? retval : 0);
1267         return retval;
1268 }
1269
1270 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1271                                     int flags, struct lov_user_md *lum,
1272                                     int lum_size)
1273 {
1274         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1275         struct ll_inode_info *lli = ll_i2info(inode);
1276         struct ptlrpc_request *req = NULL;
1277         struct obd_client_handle *och;
1278         struct lov_stripe_md *lsm;
1279         struct lustre_md md;
1280         struct file *f;
1281         int rc = 0;
1282         ENTRY;
1283
1284         
1285         if ((file->f_flags+1) & O_ACCMODE)
1286                 oit.it_flags++;
1287         if (file->f_flags & O_TRUNC)
1288                 oit.it_flags |= 2;
1289
1290         down(&lli->lli_open_sem);
1291         lsm = lli->lli_smd;
1292         if (lsm) {
1293                 up(&lli->lli_open_sem);
1294                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1295                        inode->i_ino);
1296                 RETURN(-EEXIST);
1297         }
1298
1299         f = get_empty_filp();
1300         if (!f)
1301                 GOTO(out, -ENOMEM);
1302
1303         f->f_dentry = file->f_dentry;
1304         f->f_vfsmnt = file->f_vfsmnt;
1305         f->f_flags = flags;
1306
1307         rc = ll_intent_alloc(&oit);
1308         if (rc)
1309                 GOTO(out, rc);
1310
1311         rc = ll_intent_file_open(f, lum, lum_size, &oit);
1312         if (rc)
1313                 GOTO(out, rc);
1314         if (it_disposition(&oit, DISP_LOOKUP_NEG))
1315                 GOTO(out, -ENOENT);
1316         
1317         req = LUSTRE_IT(&oit)->it_data;
1318         rc = LUSTRE_IT(&oit)->it_status;
1319
1320         if (rc < 0)
1321                 GOTO(out, rc);
1322
1323         rc = mdc_req2lustre_md(ll_i2mdexp(inode), req, 1,
1324                                ll_i2dtexp(inode), &md);
1325         if (rc)
1326                 GOTO(out, rc);
1327         ll_update_inode(f->f_dentry->d_inode, &md);
1328
1329         OBD_ALLOC(och, sizeof(struct obd_client_handle));
1330         if (!och)
1331                 GOTO(out, rc = -ENOMEM);
1332         
1333          /* actually ll_local_open() cannot fail! */
1334         rc = ll_local_open(f, &oit, och);
1335         if (rc)
1336                 GOTO(out, rc);
1337
1338         if (LUSTRE_IT(&oit)->it_lock_mode) {
1339                 ldlm_lock_decref_and_cancel((struct lustre_handle *)
1340                                             &LUSTRE_IT(&oit)->it_lock_handle,
1341                                             LUSTRE_IT(&oit)->it_lock_mode);
1342                 LUSTRE_IT(&oit)->it_lock_mode = 0;
1343         }
1344
1345         ll_intent_release(&oit);
1346
1347         /* ll_file_release() will decrease the count, but won't free anything
1348          * because we have at least one more reference coming from actual
1349          * open. */
1350         down(&lli->lli_och_sem);
1351         lli->lli_open_fd_write_count++;
1352         up(&lli->lli_och_sem);
1353         rc = ll_file_release(f->f_dentry->d_inode, f);
1354         
1355         /* Now also destroy our supplemental och */
1356         ll_md_och_close(ll_i2mdexp(inode), f->f_dentry->d_inode, och, 0, 0);
1357         EXIT;
1358 out:
1359         ll_intent_release(&oit);
1360         if (f)
1361                 put_filp(f);
1362         up(&lli->lli_open_sem);
1363         if (req != NULL)
1364                 ptlrpc_req_finished(req);
1365         return rc;
1366 }
1367
1368 static int ll_lov_setea(struct inode *inode, struct file *file,
1369                         unsigned long arg)
1370 {
1371         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1372         int lum_size = sizeof(struct lov_user_md) +
1373                 sizeof(struct lov_user_ost_data);
1374         struct lov_user_md  *lump;
1375         int rc;
1376         ENTRY;
1377
1378         if (!capable(CAP_SYS_ADMIN))
1379                 RETURN(-EPERM);
1380
1381         OBD_ALLOC(lump, lum_size);
1382         if (lump == NULL)
1383                 RETURN(-ENOMEM);
1384         
1385         rc = copy_from_user(lump, (struct lov_user_md  *)arg, lum_size);
1386         if (rc)
1387                 GOTO(out_free_lump, rc = -EFAULT);
1388
1389         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1390         EXIT;
1391 out_free_lump:
1392         OBD_FREE(lump, lum_size);
1393         return rc;
1394 }
1395
1396 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1397                             unsigned long arg)
1398 {
1399         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1400         int rc, flags = FMODE_WRITE;
1401         ENTRY;
1402
1403         /* Bug 1152: copy properly when this is no longer true */
1404         LASSERT(sizeof(lum) == sizeof(*lump));
1405         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1406         rc = copy_from_user(&lum, lump, sizeof(lum));
1407         if (rc)
1408                 RETURN(-EFAULT);
1409
1410         rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
1411         if (rc == 0) {
1412                  put_user(0, &lump->lmm_stripe_count);
1413                  rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1414                                     0, ll_i2info(inode)->lli_smd, lump);
1415         }
1416         RETURN(rc);
1417 }
1418
1419 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1420 {
1421         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1422
1423         if (!lsm)
1424                 RETURN(-ENODATA);
1425
1426         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1427                              (void *)arg);
1428 }
1429
1430 static int ll_get_grouplock(struct inode *inode, struct file *file,
1431                          unsigned long arg)
1432 {
1433         struct ll_file_data *fd = file->private_data;
1434         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1435                                                     .end = OBD_OBJECT_EOF}};
1436         struct lustre_handle lockh = { 0 };
1437         struct ll_inode_info *lli = ll_i2info(inode);
1438         struct lov_stripe_md *lsm = lli->lli_smd;
1439         int flags = 0, rc;
1440         ENTRY;
1441
1442         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1443                 RETURN(-EINVAL);
1444         }
1445
1446         policy.l_extent.gid = arg;
1447         if (file->f_flags & O_NONBLOCK)
1448                 flags = LDLM_FL_BLOCK_NOWAIT;
1449
1450         rc = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags,
1451                             &ll_i2sbi(inode)->ll_grouplock_stime);
1452         if (rc != 0)
1453                 RETURN(rc);
1454
1455         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1456         fd->fd_gid = arg;
1457         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1458
1459         RETURN(0);
1460 }
1461
1462 static int ll_put_grouplock(struct inode *inode, struct file *file,
1463                          unsigned long arg)
1464 {
1465         struct ll_file_data *fd = file->private_data;
1466         struct ll_inode_info *lli = ll_i2info(inode);
1467         struct lov_stripe_md *lsm = lli->lli_smd;
1468         int rc;
1469         ENTRY;
1470
1471         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1472                 /* Ugh, it's already unlocked. */
1473                 RETURN(-EINVAL);
1474         }
1475
1476         if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1477                 RETURN(-EINVAL);
1478
1479         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1480
1481         rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1482         if (rc)
1483                 RETURN(rc);
1484
1485         fd->fd_gid = 0;
1486         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1487
1488         RETURN(0);
1489 }
1490
1491 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1492                   unsigned long arg)
1493 {
1494         struct ll_file_data *fd = file->private_data;
1495         struct ll_sb_info *sbi = ll_i2sbi(inode);
1496         int flags;
1497         ENTRY;
1498
1499         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1500                inode->i_generation, inode, cmd);
1501
1502         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1503                 RETURN(-ENOTTY);
1504
1505         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1506         switch(cmd) {
1507         case LL_IOC_GETFLAGS:
1508                 /* Get the current value of the file flags */
1509                 return put_user(fd->fd_flags, (int *)arg);
1510         case LL_IOC_SETFLAGS:
1511         case LL_IOC_CLRFLAGS:
1512                 /* Set or clear specific file flags */
1513                 /* XXX This probably needs checks to ensure the flags are
1514                  *     not abused, and to handle any flag side effects.
1515                  */
1516                 if (get_user(flags, (int *) arg))
1517                         RETURN(-EFAULT);
1518
1519                 if (cmd == LL_IOC_SETFLAGS)
1520                         fd->fd_flags |= flags;
1521                 else
1522                         fd->fd_flags &= ~flags;
1523                 RETURN(0);
1524         case LL_IOC_LOV_SETSTRIPE:
1525                 RETURN(ll_lov_setstripe(inode, file, arg));
1526         case LL_IOC_LOV_SETEA:
1527                 RETURN(ll_lov_setea(inode, file, arg));
1528         case IOC_MDC_SHOWFID: {
1529                 struct lustre_id *idp = (struct lustre_id *)arg;
1530                 struct lustre_id id;
1531                 char *filename;
1532                 int rc;
1533
1534                 filename = getname((const char *)arg);
1535                 if (IS_ERR(filename))
1536                         RETURN(PTR_ERR(filename));
1537
1538                 ll_inode2id(&id, inode);
1539
1540                 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
1541                 if (rc < 0)
1542                         GOTO(out_filename, rc);
1543
1544                 rc = copy_to_user(idp, &id, sizeof(*idp));
1545                 if (rc)
1546                         GOTO(out_filename, rc = -EFAULT);
1547
1548                 EXIT;
1549         out_filename:
1550                 putname(filename);
1551                 return rc;
1552         }
1553         case LL_IOC_KEY_TYPE: {
1554                 struct obd_ioctl_data *data;
1555                 char *buf = NULL;
1556                 char *type;
1557                 int typelen, rc, len = 0;
1558
1559                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1560                 if (rc)
1561                         RETURN(rc);
1562                 data = (void *)buf;
1563
1564                 type = data->ioc_inlbuf1;
1565                 typelen = data->ioc_inllen1;
1566
1567                 if (typelen < 1) {
1568                         CDEBUG(D_INFO, "LL_IOC_KEY_TYPE missing filename\n");
1569                         GOTO(out, rc = -EINVAL);
1570                 }
1571                 ll_set_sb_gksinfo(inode->i_sb, type);
1572                 EXIT;
1573         out:
1574                 obd_ioctl_freedata(buf, len);
1575                 return rc;
1576         }
1577
1578         case LL_IOC_LOV_GETSTRIPE:
1579                 RETURN(ll_lov_getstripe(inode, arg));
1580         case EXT3_IOC_GETFLAGS:
1581         case EXT3_IOC_SETFLAGS:
1582                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1583         case LL_IOC_GROUP_LOCK:
1584                 RETURN(ll_get_grouplock(inode, file, arg));
1585         case LL_IOC_GROUP_UNLOCK:
1586                 RETURN(ll_put_grouplock(inode, file, arg));
1587         case EXT3_IOC_GETVERSION_OLD:
1588         case EXT3_IOC_GETVERSION:
1589                 return put_user(inode->i_generation, (int *) arg);
1590         /* We need to special case any other ioctls we want to handle,
1591          * to send them to the MDS/OST as appropriate and to properly
1592          * network encode the arg field.
1593         case EXT2_IOC_GETVERSION_OLD:
1594         case EXT2_IOC_GETVERSION_NEW:
1595         case EXT2_IOC_SETVERSION_OLD:
1596         case EXT2_IOC_SETVERSION_NEW:
1597         case EXT3_IOC_SETVERSION_OLD:
1598         case EXT3_IOC_SETVERSION:
1599         */
1600         case LL_IOC_FLUSH_CRED:
1601                 RETURN(ll_flush_cred(inode));
1602         case LL_IOC_AUDIT:
1603                 RETURN(ll_set_audit(inode, arg));
1604         default:
1605                 RETURN( obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1606                                       (void *)arg) );
1607         }
1608 }
1609
1610 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1611 {
1612         struct inode *inode = file->f_dentry->d_inode;
1613         struct ll_file_data *fd = file->private_data;
1614         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1615         struct lustre_handle lockh = {0};
1616         loff_t retval;
1617         ENTRY;
1618         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1619                inode->i_generation, inode,
1620                offset + ((origin==2) ? inode->i_size : file->f_pos));
1621
1622         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1623         if (origin == 2) { /* SEEK_END */
1624                 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1625                 struct ll_inode_info *lli = ll_i2info(inode);
1626                 int nonblock = 0, rc;
1627
1628                 if (file->f_flags & O_NONBLOCK)
1629                         nonblock = LDLM_FL_BLOCK_NOWAIT;
1630
1631                 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1632                                     nonblock, &ll_i2sbi(inode)->ll_seek_stime);
1633                 if (rc != 0)
1634                         RETURN(rc);
1635
1636                 down(&lli->lli_size_sem);
1637                 offset += inode->i_size;
1638                 up(&lli->lli_size_sem);
1639         } else if (origin == 1) { /* SEEK_CUR */
1640                 offset += file->f_pos;
1641         }
1642
1643         retval = -EINVAL;
1644         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1645                 if (offset != file->f_pos) {
1646                         file->f_pos = offset;
1647 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1648                         file->f_reada = 0;
1649                         file->f_version = ++event;
1650 #endif
1651                 }
1652                 retval = offset;
1653         }
1654
1655         if (origin == 2)
1656                 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1657         RETURN(retval);
1658 }
1659
1660 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1661 {
1662         struct inode *inode = dentry->d_inode;
1663         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1664         struct lustre_id id;
1665         struct ptlrpc_request *req;
1666         int rc, err;
1667         ENTRY;
1668         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1669                inode->i_generation, inode);
1670
1671         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1672
1673         /* fsync's caller has already called _fdata{sync,write}, we want
1674          * that IO to finish before calling the osc and mdc sync methods */
1675         rc = filemap_fdatawait(inode->i_mapping);
1676
1677         ll_inode2id(&id, inode);
1678         err = md_sync(ll_i2sbi(inode)->ll_md_exp, &id, &req);
1679         if (!rc)
1680                 rc = err;
1681         if (!err)
1682                 ptlrpc_req_finished(req);
1683
1684         if (data && lsm) {
1685                 struct obdo *oa = obdo_alloc();
1686
1687                 if (!oa)
1688                         RETURN(rc ? rc : -ENOMEM);
1689
1690                 oa->o_id = lsm->lsm_object_id;
1691                 oa->o_gr = lsm->lsm_object_gr;
1692                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1693
1694                 obdo_from_inode(oa, inode, (OBD_MD_FLTYPE | OBD_MD_FLATIME |
1695                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1696                                             OBD_MD_FLGROUP));
1697
1698                 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1699                                0, OBD_OBJECT_EOF);
1700                 if (!rc)
1701                         rc = err;
1702                 obdo_free(oa);
1703         }
1704
1705         RETURN(rc);
1706 }
1707
1708 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1709 {
1710         struct inode *inode = file->f_dentry->d_inode;
1711         struct ll_inode_info *li = ll_i2info(inode);
1712         struct ll_sb_info *sbi = ll_i2sbi(inode);
1713         struct obd_device *obddev;
1714         struct ldlm_res_id res_id =
1715                 { .name = {id_fid(&li->lli_id), id_group(&li->lli_id), LDLM_FLOCK} };
1716         struct lustre_handle lockh = {0};
1717         ldlm_policy_data_t flock;
1718         ldlm_mode_t mode = 0;
1719         int flags = 0;
1720         int rc;
1721         ENTRY;
1722
1723         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1724                inode->i_ino, file_lock);
1725
1726         flock.l_flock.pid = file_lock->fl_pid;
1727         flock.l_flock.start = file_lock->fl_start;
1728         flock.l_flock.end = file_lock->fl_end;
1729
1730         switch (file_lock->fl_type) {
1731         case F_RDLCK:
1732                 mode = LCK_PR;
1733                 break;
1734         case F_UNLCK:
1735                 /* An unlock request may or may not have any relation to
1736                  * existing locks so we may not be able to pass a lock handle
1737                  * via a normal ldlm_lock_cancel() request. The request may even
1738                  * unlock a byte range in the middle of an existing lock. In
1739                  * order to process an unlock request we need all of the same
1740                  * information that is given with a normal read or write record
1741                  * lock request. To avoid creating another ldlm unlock (cancel)
1742                  * message we'll treat a LCK_NL flock request as an unlock. */
1743                 mode = LCK_NL;
1744                 break;
1745         case F_WRLCK:
1746                 mode = LCK_PW;
1747                 break;
1748         default:
1749                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1750                 LBUG();
1751         }
1752
1753         switch (cmd) {
1754         case F_SETLKW:
1755 #ifdef F_SETLKW64
1756         case F_SETLKW64:
1757 #endif
1758                 flags = 0;
1759                 break;
1760         case F_SETLK:
1761 #ifdef F_SETLK64
1762         case F_SETLK64:
1763 #endif
1764                 flags = LDLM_FL_BLOCK_NOWAIT;
1765                 break;
1766         case F_GETLK:
1767 #ifdef F_GETLK64
1768         case F_GETLK64:
1769 #endif
1770                 flags = LDLM_FL_TEST_LOCK;
1771                 /* Save the old mode so that if the mode in the lock changes we
1772                  * can decrement the appropriate reader or writer refcount. */
1773                 file_lock->fl_type = mode;
1774                 break;
1775         default:
1776                 CERROR("unknown fcntl lock command: %d\n", cmd);
1777                 LBUG();
1778         }
1779
1780         CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1781                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1782                flags, mode, flock.l_flock.start, flock.l_flock.end);
1783
1784         obddev = md_get_real_obd(sbi->ll_md_exp, &li->lli_id);
1785         rc = ldlm_cli_enqueue(obddev->obd_self_export, NULL,
1786                               obddev->obd_namespace,
1787                               res_id, LDLM_FLOCK, &flock, mode, &flags,
1788                               NULL, ldlm_flock_completion_ast, NULL, file_lock,
1789                               NULL, 0, NULL, &lockh);
1790         RETURN(rc);
1791 }
1792
1793 int ll_inode_revalidate_it(struct dentry *dentry)
1794 {
1795         struct lookup_intent oit = { .it_op = IT_GETATTR };
1796         struct inode *inode = dentry->d_inode;
1797         struct ptlrpc_request *req = NULL;
1798         struct ll_inode_info *lli;
1799         struct lov_stripe_md *lsm;
1800         struct ll_sb_info *sbi;
1801         struct lustre_id id;
1802         int rc;
1803         ENTRY;
1804
1805         if (!inode) {
1806                 CERROR("REPORT THIS LINE TO PETER\n");
1807                 RETURN(0);
1808         }
1809         
1810         sbi = ll_i2sbi(inode);
1811         
1812         ll_inode2id(&id, inode);
1813         lli = ll_i2info(inode);
1814         LASSERT(id_fid(&id) != 0);
1815
1816         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), name=%s(%p)\n",
1817                inode->i_ino, inode->i_generation, inode, dentry->d_name.name,
1818                dentry);
1819
1820 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1821         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1822 #endif
1823
1824         rc = ll_intent_alloc(&oit);
1825         if (rc)
1826                 RETURN(-ENOMEM);
1827         
1828         rc = ll_crypto_init_it_key(inode, &oit);
1829         if (rc)
1830                 GOTO(out, rc);
1831
1832         rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1833                             &oit, 0, &req, ll_mdc_blocking_ast);
1834         if (rc < 0)
1835                 GOTO(out, rc);
1836
1837         rc = revalidate_it_finish(req, 1, &oit, dentry);
1838         if (rc) {
1839                 GOTO(out, rc);
1840         }
1841
1842         ll_lookup_finish_locks(&oit, dentry);
1843
1844         if (!LLI_HAVE_FLSIZE(inode)) {
1845                 /* if object not yet allocated, don't validate size */
1846                 lsm = lli->lli_smd;
1847                 if (lsm != NULL) {
1848                         /* ll_glimpse_size() will prefer locally cached
1849                          * writes if they extend the file */
1850                         rc = ll_glimpse_size(inode);
1851                 }
1852         }
1853         EXIT;
1854 out:
1855         ll_intent_release(&oit);
1856         if (req)
1857                 ptlrpc_req_finished(req);
1858         return rc;
1859 }
1860
1861 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1862 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
1863 {
1864         int res = 0;
1865         struct inode *inode = de->d_inode;
1866         struct ll_inode_info *lli = ll_i2info(inode);
1867
1868         res = ll_inode_revalidate_it(de);
1869         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1870
1871         if (res)
1872                 return res;
1873
1874         stat->ino = inode->i_ino;
1875         stat->mode = inode->i_mode;
1876         stat->nlink = inode->i_nlink;
1877         stat->uid = inode->i_uid;
1878         stat->gid = inode->i_gid;
1879         stat->atime = inode->i_atime;
1880         stat->mtime = inode->i_mtime;
1881         stat->ctime = inode->i_ctime;
1882         stat->blksize = inode->i_blksize;
1883
1884         down(&lli->lli_size_sem);
1885         stat->size = inode->i_size;
1886         stat->blocks = inode->i_blocks;
1887         up(&lli->lli_size_sem);
1888         
1889         stat->rdev = kdev_t_to_nr(inode->i_rdev);
1890         stat->dev = id_group(&ll_i2info(inode)->lli_id);
1891         return 0;
1892 }
1893 #endif
1894
1895 static
1896 int ll_setxattr_internal(struct inode *inode, const char *name,
1897                          const void *value, size_t size, int flags, 
1898                          __u64 valid)
1899 {
1900         struct ll_sb_info *sbi = ll_i2sbi(inode);
1901         struct ptlrpc_request *request = NULL;
1902         struct mdc_op_data *op_data;
1903         struct iattr attr;
1904         void *key = NULL;
1905         int rc = 0, key_size = 0;
1906         ENTRY;
1907
1908         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1909         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETXATTR);
1910
1911         if (sbi->ll_remote && !strcmp(name, XATTR_NAME_ACL_ACCESS))
1912                 RETURN(-EOPNOTSUPP);
1913
1914         memset(&attr, 0x0, sizeof(attr));
1915         attr.ia_valid |= valid;
1916         attr.ia_attr_flags = flags;
1917
1918         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1919                 rc = ll_crypto_get_mac(inode, &attr, (void *)value, size, 
1920                                        &key, &key_size);
1921                 if (rc) {
1922                         CERROR("can not get right mac, rc=%d\n", rc);
1923                         GOTO(out, rc);
1924                 }
1925         }
1926
1927         OBD_ALLOC(op_data, sizeof(*op_data));
1928         if (!op_data)
1929                 RETURN(-ENOMEM);
1930         
1931         ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
1932
1933         rc = md_setattr(sbi->ll_md_exp, op_data, &attr,
1934                         (void *)name, strnlen(name, XATTR_NAME_MAX) + 1, 
1935                         (void *)value,  size, key, key_size, &request);
1936         OBD_FREE(op_data, sizeof(*op_data));
1937         
1938         if (rc) {
1939                 CDEBUG(D_SEC, "md_setattr fails: rc = %d\n", rc);
1940                 GOTO(out, rc);
1941         }
1942 out:
1943         if (key && key_size) 
1944                 OBD_FREE(key, key_size);
1945         ptlrpc_req_finished(request);
1946         RETURN(rc);
1947 }
1948
1949 int ll_setxattr(struct dentry *dentry, const char *name, const void *value,
1950                 size_t size, int flags)
1951 {
1952         int rc, error;
1953         struct posix_acl *acl;
1954         struct ll_inode_info *lli;
1955         ENTRY;
1956
1957         rc = ll_setxattr_internal(dentry->d_inode, name, value, size, 
1958                                   flags, ATTR_EA);
1959
1960         /* update inode's acl info */
1961         if (rc == 0 && strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1962                 if (value) {
1963                         acl = posix_acl_from_xattr(value, size);
1964                         if (IS_ERR(acl)) {
1965                                 CERROR("convert from xattr to acl error: %ld",
1966                                         PTR_ERR(acl));
1967                                 GOTO(out, rc);
1968                         } else if (acl) {
1969                                 error = posix_acl_valid(acl);
1970                                 if (error) {
1971                                         CERROR("acl valid error: %d", error);
1972                                         posix_acl_release(acl);
1973                                         GOTO(out, rc);
1974                                 }
1975                         }
1976                 } else {
1977                         acl = NULL;
1978                 }
1979                                         
1980                 lli = ll_i2info(dentry->d_inode);
1981                 spin_lock(&lli->lli_lock);
1982                 if (lli->lli_posix_acl != NULL)
1983                         posix_acl_release(lli->lli_posix_acl);
1984                 lli->lli_posix_acl = acl;
1985                 spin_unlock(&lli->lli_lock);
1986         }
1987         EXIT;
1988 out:
1989         return(rc);
1990 }
1991
1992 int ll_removexattr(struct dentry *dentry, const char *name)
1993 {
1994         return ll_setxattr_internal(dentry->d_inode, name, NULL, 0, 0,
1995                                     ATTR_EA_RM);
1996 }
1997
1998 int ll_getxattr_internal(struct inode *inode, const char *name,
1999                          void *value, size_t size, __u64 valid)
2000 {
2001         struct ptlrpc_request *request = NULL;
2002         struct ll_sb_info *sbi = ll_i2sbi(inode);
2003         struct lustre_id id;
2004         struct mds_body *body;
2005         void *ea_data; 
2006         int rc, ea_size;
2007         ENTRY;
2008
2009         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETXATTR);
2010
2011         if (sbi->ll_remote && !strcmp(name, XATTR_NAME_ACL_ACCESS))
2012                 RETURN(-EOPNOTSUPP);
2013
2014         ll_inode2id(&id, inode);
2015         rc = md_getattr(sbi->ll_md_exp, &id, valid, name, NULL, 0,
2016                         size, NULL, &request);
2017         if (rc) {
2018                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
2019                         CERROR("rc = %d\n", rc);
2020                 GOTO(out, rc);
2021         }
2022
2023         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
2024         LASSERT(body != NULL);
2025         LASSERT_REPSWABBED(request, 0);
2026
2027         ea_size = body->eadatasize;
2028         if (size == 0) 
2029                 GOTO(out, rc = ea_size);
2030
2031         LASSERT(ea_size <= request->rq_repmsg->buflens[1]);
2032         ea_data = lustre_msg_buf(request->rq_repmsg, 1, ea_size);
2033         LASSERT(ea_data != NULL);
2034         LASSERT_REPSWABBED(request, 1);
2035
2036         if (value)
2037                 memcpy(value, ea_data, ea_size);
2038         rc = ea_size;
2039
2040  out:
2041         ptlrpc_req_finished(request);
2042         RETURN(rc);
2043 }
2044
2045 int ll_getxattr(struct dentry *dentry, const char *name, void *value,
2046                 size_t size)
2047 {
2048         return ll_getxattr_internal(dentry->d_inode, name,
2049                                     value, size, OBD_MD_FLXATTR);
2050 }
2051
2052 int ll_listxattr(struct dentry *dentry, char *list, size_t size)
2053 {
2054         return ll_getxattr_internal(dentry->d_inode, NULL, list, size,
2055                                     OBD_MD_FLXATTRLIST);
2056 }
2057
2058 /*
2059  * Here we hold DLM lock across permission check, to get better
2060  * conformance especially for remote acl. will introduce certain
2061  * overhead, maybe need a profile.
2062  */
2063 static int
2064 lustre_check_acl(struct inode *inode, int mask)
2065 {
2066         struct lookup_intent it = { .it_op = IT_GETATTR };
2067         struct dentry de = { .d_inode = inode };
2068         struct ll_sb_info *sbi;
2069         struct lustre_id id;
2070         struct ptlrpc_request *req = NULL;
2071         struct ll_inode_info *lli = ll_i2info(inode);
2072         struct posix_acl *acl;
2073         int rc = 0;
2074         ENTRY;
2075
2076         sbi = ll_i2sbi(inode);
2077         ll_inode2id(&id, inode);
2078
2079         if (ll_intent_alloc(&it))
2080                 return -EACCES;
2081
2082         rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
2083                             &it, 0, &req, ll_mdc_blocking_ast);
2084         if (rc < 0) {
2085                 ll_intent_free(&it);
2086                 GOTO(out, rc);
2087         }
2088
2089         rc = revalidate_it_finish(req, 1, &it, &de);
2090         if (rc) {
2091                 ll_intent_release(&it);
2092                 GOTO(out, rc);
2093         }
2094
2095         if (sbi->ll_remote) {
2096                 rc = ll_remote_acl_permission(inode, mask);
2097         } else {
2098                 spin_lock(&lli->lli_lock);
2099                 acl = posix_acl_dup(ll_i2info(inode)->lli_posix_acl);
2100                 spin_unlock(&lli->lli_lock);
2101
2102                 if (!acl)
2103                         rc = -EAGAIN;
2104                 else {
2105                         rc = posix_acl_permission(inode, acl, mask);
2106                         posix_acl_release(acl);
2107                 }
2108         }
2109
2110         ll_lookup_finish_locks(&it, &de);
2111         ll_intent_free(&it);
2112
2113 out:
2114         if (req)
2115                 ptlrpc_req_finished(req);
2116
2117         RETURN(rc);
2118 }
2119
2120 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2121 {
2122         struct ll_sb_info *sbi = ll_i2sbi(inode);
2123
2124         /* for remote client, permission bits in inode doesn't
2125          * play a role anymore.
2126          */
2127         if (sbi->ll_remote)
2128                 return lustre_check_acl(inode, mask);
2129
2130         return generic_permission(inode, mask, lustre_check_acl);
2131 }
2132
2133 struct file_operations ll_file_operations = {
2134         .read           = ll_file_read,
2135         .write          = ll_file_write,
2136         .ioctl          = ll_file_ioctl,
2137         .open           = ll_file_open,
2138         .release        = ll_file_release,
2139         .mmap           = ll_file_mmap,
2140         .llseek         = ll_file_seek,
2141 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
2142         .sendfile       = generic_file_sendfile,
2143 #endif
2144         .fsync          = ll_fsync,
2145         .lock           = ll_file_flock
2146 };
2147
2148 struct inode_operations ll_file_inode_operations = {
2149         .setattr        = ll_setattr,
2150         .truncate       = ll_truncate,
2151 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2152         .getattr        = ll_getattr,
2153 #else
2154         .revalidate_it  = ll_inode_revalidate_it,
2155 #endif
2156         .setxattr       = ll_setxattr,
2157         .getxattr       = ll_getxattr,
2158         .listxattr      = ll_listxattr,
2159         .removexattr    = ll_removexattr,
2160         .permission     = ll_inode_permission,
2161 };
2162