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