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