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