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