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