Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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
27 #include <linux/lustre_dlm.h>
28 #include <linux/lustre_lite.h>
29 #include <linux/obd_lov.h>      /* for lov_mds_md_size() in lov_setstripe() */
30 #include <linux/random.h>
31
32 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
33 extern int ll_setattr(struct dentry *de, struct iattr *attr);
34
35 static int ll_mdc_close(struct lustre_handle *mdc_conn, struct inode *inode,
36                         struct file *file)
37 {
38         struct ll_file_data *fd = file->private_data;
39         struct ptlrpc_request *req = NULL;
40         unsigned long flags;
41         struct obd_import *imp;
42         int rc;
43         ENTRY;
44
45         /* Complete the open request and remove it from replay list */
46         rc = mdc_close(&ll_i2sbi(inode)->ll_mdc_conn, inode->i_ino,
47                        inode->i_mode, &fd->fd_mdshandle, &req);
48         if (rc)
49                 CERROR("inode %lu close failed: rc = %d\n", inode->i_ino, rc);
50
51         imp = fd->fd_req->rq_import;
52         LASSERT(imp != NULL);
53         spin_lock_irqsave(&imp->imp_lock, flags);
54
55         DEBUG_REQ(D_HA, fd->fd_req, "matched open req %p", fd->fd_req);
56
57         /* We held on to the request for replay until we saw a close for that
58          * file.  Now that we've closed it, it gets replayed on the basis of
59          * its transno only. */
60         fd->fd_req->rq_flags &= ~PTL_RPC_FL_REPLAY;
61
62         if (fd->fd_req->rq_transno) {
63                 /* This open created a file, so it needs replay as a
64                  * normal transaction now.  Our reference to it now
65                  * effectively owned by the imp_replay_list, and it'll
66                  * be committed just like other transno-having
67                  * requests from here on out. */
68
69                 /* We now retain this close request, so that it is
70                  * replayed if the open is replayed.  We duplicate the
71                  * transno, so that we get freed at the right time,
72                  * and rely on the difference in xid to keep
73                  * everything ordered correctly.
74                  *
75                  * But! If this close was already given a transno
76                  * (because it caused real unlinking of an
77                  * open-unlinked file, f.e.), then we'll be ordered on
78                  * the basis of that and we don't need to do anything
79                  * magical here. */
80                 if (!req->rq_transno) {
81                         req->rq_transno = fd->fd_req->rq_transno;
82                         ptlrpc_retain_replayable_request(req, imp);
83                 }
84                 spin_unlock_irqrestore(&imp->imp_lock, flags);
85
86                 /* Should we free_committed now? we always free before
87                  * replay, so it's probably a wash.  We could check to
88                  * see if the fd_req should already be committed, in
89                  * which case we can avoid the whole retain_replayable
90                  * dance. */
91         } else {
92                 /* No transno means that we can just drop our ref. */
93                 spin_unlock_irqrestore(&imp->imp_lock, flags);
94         }
95         ptlrpc_req_finished(fd->fd_req);
96
97         /* Do this after the fd_req->rq_transno check, because we don't want
98          * to bounce off zero references. */
99         ptlrpc_req_finished(req);
100         fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
101         file->private_data = NULL;
102         kmem_cache_free(ll_file_data_slab, fd);
103
104         RETURN(-abs(rc));
105 }
106
107 /* While this returns an error code, fput() the caller does not, so we need
108  * to make every effort to clean up all of our state here.  Also, applications
109  * rarely check close errors and even if an error is returned they will not
110  * re-try the close call.
111  */
112 static int ll_file_release(struct inode *inode, struct file *file)
113 {
114         struct ll_file_data *fd;
115         struct obdo oa;
116         struct ll_sb_info *sbi = ll_i2sbi(inode);
117         struct ll_inode_info *lli = ll_i2info(inode);
118         struct lov_stripe_md *lsm = lli->lli_smd;
119         int rc = 0, rc2;
120
121         ENTRY;
122
123         fd = (struct ll_file_data *)file->private_data;
124         if (!fd) /* no process opened the file after an mcreate */
125                 RETURN(rc = 0);
126
127         if (lsm != NULL) {
128                 memset(&oa, 0, sizeof(oa));
129                 oa.o_id = lsm->lsm_object_id;
130                 oa.o_mode = S_IFREG;
131                 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
132
133                 memcpy(&oa.o_inline, fd->fd_ostdata, FD_OSTDATA_SIZE);
134                 oa.o_valid |= OBD_MD_FLHANDLE;
135
136                 rc = obd_close(&sbi->ll_osc_conn, &oa, lsm, NULL);
137                 if (rc)
138                         CERROR("inode %lu object close failed: rc = %d\n",
139                                inode->i_ino, rc);
140         }
141
142         rc2 = ll_mdc_close(&sbi->ll_mdc_conn, inode, file);
143         if (rc2 && !rc)
144                 rc = rc2;
145
146         RETURN(rc);
147 }
148
149 static int ll_local_open(struct file *file, struct lookup_intent *it)
150 {
151         struct ptlrpc_request *req = it->it_data;
152         struct ll_file_data *fd;
153         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 1);
154         ENTRY;
155
156         LASSERT(!file->private_data);
157
158         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
159         /* We can't handle this well without reorganizing ll_file_open and
160          * ll_mdc_close, so don't even try right now. */
161         LASSERT(fd != NULL);
162
163         memset(fd, 0, sizeof(*fd));
164
165         memcpy(&fd->fd_mdshandle, &body->handle, sizeof(body->handle));
166         fd->fd_req = it->it_data;
167         file->private_data = fd;
168
169         RETURN(0);
170 }
171
172 static int ll_osc_open(struct lustre_handle *conn, struct inode *inode,
173                        struct file *file, struct lov_stripe_md *lsm)
174 {
175         struct ll_file_data *fd = file->private_data;
176         struct obdo *oa;
177         int rc;
178         ENTRY;
179
180         oa = obdo_alloc();
181         if (!oa)
182                 RETURN(-ENOMEM);
183         oa->o_id = lsm->lsm_object_id;
184         oa->o_mode = S_IFREG;
185         oa->o_valid = (OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
186                        OBD_MD_FLBLOCKS | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
187         rc = obd_open(conn, oa, lsm, NULL);
188         if (rc)
189                 GOTO(out, rc);
190
191         file->f_flags &= ~O_LOV_DELAY_CREATE;
192         obdo_to_inode(inode, oa, (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
193                                   OBD_MD_FLMTIME | OBD_MD_FLCTIME));
194
195         if (oa->o_valid |= OBD_MD_FLHANDLE)
196                 memcpy(fd->fd_ostdata, obdo_handle(oa), FD_OSTDATA_SIZE);
197
198         EXIT;
199 out:
200         obdo_free(oa);
201         return rc;
202 }
203
204 /* Caller must hold lli_open_sem to protect lli->lli_smd from changing and
205  * duplicate objects from being created.  We only install lsm to lli_smd if
206  * the mdc open was successful (hence stored stripe MD on MDS), otherwise
207  * other nodes could try to create different objects for the same file.
208  */
209 static int ll_create_obj(struct lustre_handle *conn, struct inode *inode,
210                          struct file *file, struct lov_stripe_md *lsm)
211 {
212         struct ptlrpc_request *req = NULL;
213         struct ll_inode_info *lli = ll_i2info(inode);
214         struct lov_mds_md *lmm = NULL;
215         struct obdo *oa;
216         struct iattr iattr;
217         int rc, err, lmm_size = 0;;
218         ENTRY;
219
220         oa = obdo_alloc();
221         if (!oa)
222                 RETURN(-ENOMEM);
223
224         oa->o_mode = S_IFREG | 0600;
225         oa->o_id = inode->i_ino;
226         /* Keep these 0 for now, because chown/chgrp does not change the
227          * ownership on the OST, and we don't want to allow BA OST NFS
228          * users to access these objects by mistake.
229          */
230         oa->o_uid = 0;
231         oa->o_gid = 0;
232         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
233                 OBD_MD_FLUID | OBD_MD_FLGID;
234
235         rc = obd_create(conn, oa, &lsm, NULL);
236         if (rc) {
237                 CERROR("error creating objects for inode %lu: rc = %d\n",
238                        inode->i_ino, rc);
239                 if (rc > 0) {
240                         CERROR("obd_create returned invalid rc %d\n", rc);
241                         rc = -EIO;
242                 }
243                 GOTO(out_oa, rc);
244         }
245
246         LASSERT(lsm && lsm->lsm_object_id);
247         rc = obd_packmd(conn, &lmm, lsm);
248         if (rc < 0)
249                 GOTO(out_destroy, rc);
250
251         lmm_size = rc;
252
253         /* Save the stripe MD with this file on the MDS */
254         memset(&iattr, 0, sizeof(iattr));
255         iattr.ia_valid = ATTR_FROM_OPEN;
256         rc = mdc_setattr(&ll_i2sbi(inode)->ll_mdc_conn, inode, &iattr,
257                          lmm, lmm_size, &req);
258         ptlrpc_req_finished(req);
259
260         obd_free_wiremd(conn, &lmm);
261
262         /* If we couldn't complete mdc_open() and store the stripe MD on the
263          * MDS, we need to destroy the objects now or they will be leaked.
264          */
265         if (rc) {
266                 CERROR("error: storing stripe MD for %lu: rc %d\n",
267                        inode->i_ino, rc);
268                 GOTO(out_destroy, rc);
269         }
270         lli->lli_smd = lsm;
271
272         EXIT;
273 out_oa:
274         obdo_free(oa);
275         return rc;
276
277 out_destroy:
278         obdo_from_inode(oa, inode, OBD_MD_FLTYPE);
279         oa->o_id = lsm->lsm_object_id;
280         oa->o_valid |= OBD_MD_FLID;
281         err = obd_destroy(conn, oa, lsm, NULL);
282         obd_free_memmd(conn, &lsm);
283         if (err)
284                 CERROR("error uncreating inode %lu objects: rc %d\n",
285                        inode->i_ino, err);
286         goto out_oa;
287 }
288
289 /* Open a file, and (for the very first open) create objects on the OSTs at
290  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
291  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
292  * lli_open_sem to ensure no other process will create objects, send the
293  * stripe MD to the MDS, or try to destroy the objects if that fails.
294  *
295  * If we already have the stripe MD locally then we don't request it in
296  * mdc_open(), by passing a lmm_size = 0.
297  *
298  * It is up to the application to ensure no other processes open this file
299  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
300  * used.  We might be able to avoid races of that sort by getting lli_open_sem
301  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
302  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
303  */
304 extern int ll_it_open_error(int phase, struct lookup_intent *it);
305
306 static int ll_file_open(struct inode *inode, struct file *file)
307 {
308         struct ll_sb_info *sbi = ll_i2sbi(inode);
309         struct ll_inode_info *lli = ll_i2info(inode);
310         struct lustre_handle *conn = ll_i2obdconn(inode);
311         struct lookup_intent *it;
312         struct lov_stripe_md *lsm;
313         int rc = 0;
314         ENTRY;
315
316         CDEBUG(D_VFSTRACE, "VFS Op\n");
317         LL_GET_INTENT(file->f_dentry, it);
318         rc = ll_it_open_error(IT_OPEN_OPEN, it);
319         if (rc)
320                 RETURN(rc);
321
322         rc = ll_local_open(file, it);
323         if (rc)
324                 LBUG();
325
326         mdc_set_open_replay_data((struct ll_file_data *)file->private_data);
327
328         lsm = lli->lli_smd;
329         if (lsm == NULL) {
330                 if (file->f_flags & O_LOV_DELAY_CREATE) {
331                         CDEBUG(D_INODE, "delaying object creation\n");
332                         RETURN(0);
333                 }
334                 down(&lli->lli_open_sem);
335                 if (!lli->lli_smd) {
336                         rc = ll_create_obj(conn, inode, file, NULL);
337                         up(&lli->lli_open_sem);
338                         if (rc)
339                                 GOTO(out_close, rc);
340                 } else {
341                         CERROR("warning: stripe already set on ino %lu\n",
342                                inode->i_ino);
343                         up(&lli->lli_open_sem);
344                 }
345                 lsm = lli->lli_smd;
346         }
347
348         rc = ll_osc_open(conn, inode, file, lsm);
349         if (rc)
350                 GOTO(out_close, rc);
351         RETURN(0);
352
353  out_close:
354         ll_mdc_close(&sbi->ll_mdc_conn, inode, file);
355         return rc;
356 }
357
358 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
359                  int mode, struct lustre_handle *lockh)
360 {
361         struct ll_sb_info *sbi = ll_i2sbi(inode);
362         struct ldlm_extent extent;
363         int rc, flags = 0;
364         ENTRY;
365
366         /* XXX phil: can we do this?  won't it screw the file size up? */
367         if (sbi->ll_flags & LL_SBI_NOLCK)
368                 RETURN(0);
369
370         extent.start = start;
371         extent.end = OBD_OBJECT_EOF;
372
373         rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
374                          sizeof(extent), mode, &flags, ll_lock_callback,
375                          inode, sizeof(*inode), lockh);
376         RETURN(rc);
377 }
378
379 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
380                    struct lustre_handle *lockh)
381 {
382         struct ll_sb_info *sbi = ll_i2sbi(inode);
383         int rc;
384         ENTRY;
385
386         /* XXX phil: can we do this?  won't it screw the file size up? */
387         if (sbi->ll_flags & LL_SBI_NOLCK)
388                 RETURN(0);
389
390         rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockh);
391         if (rc != ELDLM_OK) {
392                 CERROR("lock cancel: %d\n", rc);
393                 LBUG();
394         }
395
396         RETURN(rc);
397 }
398
399 /* This function is solely "sampling" the file size, and does not explicit
400  * locking on the size itself (see ll_size_lock() and ll_size_unlock()).
401  *
402  * XXX We need to optimize away the obd_getattr for decent performance here,
403  *     by checking if we already have the size lock and considering our size
404  *     authoritative in that case.  In order to do that either the act of
405  *     getting the size lock includes retrieving the file size, or the client
406  *     keeps an atomic flag in the inode which indicates whether the size
407  *     has been updated (see bug 280).
408  */
409 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm, char *ostdata)
410 {
411         struct ll_sb_info *sbi = ll_i2sbi(inode);
412         struct obdo oa;
413         int rc;
414         ENTRY;
415
416         LASSERT(lsm);
417         LASSERT(sbi);
418
419         memset(&oa, 0, sizeof oa);
420         oa.o_id = lsm->lsm_object_id;
421         oa.o_mode = S_IFREG;
422         oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
423                 OBD_MD_FLBLOCKS | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
424
425         if (ostdata != NULL) {
426                 memcpy(&oa.o_inline, ostdata, FD_OSTDATA_SIZE);
427                 oa.o_valid |= OBD_MD_FLHANDLE;
428         }
429
430         rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
431         if (!rc) {
432                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
433                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
434                 CDEBUG(D_INODE, "objid "LPX64" size %Lu/%Lx\n",
435                        lsm->lsm_object_id, inode->i_size, inode->i_size);
436         }
437
438         RETURN(rc);
439 }
440
441 static inline void ll_remove_suid(struct inode *inode)
442 {
443         unsigned int mode;
444
445         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
446         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
447
448         /* was any of the uid bits set? */
449         mode &= inode->i_mode;
450         if (mode && !capable(CAP_FSETID)) {
451                 inode->i_mode &= ~mode;
452                 // XXX careful here - we cannot change the size
453         }
454 }
455
456 static void ll_update_atime(struct inode *inode)
457 {
458 #ifdef USE_ATIME
459         struct iattr attr;
460
461 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
462         attr.ia_atime = CURRENT_TIME;
463 #else
464         attr.ia_atime = CURRENT_TIME.tv_sec;
465 #endif
466         attr.ia_valid = ATTR_ATIME;
467
468         if (inode->i_atime == attr.ia_atime) return;
469         if (IS_RDONLY(inode)) return;
470         if (IS_NOATIME(inode)) return;
471
472         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
473         ll_inode_setattr(inode, &attr, 0);
474 #else
475         /* update atime, but don't explicitly write it out just this change */
476         inode->i_atime = CURRENT_TIME;
477 #endif
478 }
479
480 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
481                      void *data, int flag)
482 {
483         struct inode *inode = data;
484         struct lustre_handle lockh = { 0, 0 };
485         int rc;
486         ENTRY;
487         CDEBUG(D_VFSTRACE, "VFS Op\n");
488
489         if (inode == NULL)
490                 LBUG();
491
492         switch (flag) {
493         case LDLM_CB_BLOCKING:
494                 ldlm_lock2handle(lock, &lockh);
495                 rc = ldlm_cli_cancel(&lockh);
496                 if (rc != ELDLM_OK)
497                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
498                 break;
499         case LDLM_CB_CANCELING:
500                 CDEBUG(D_INODE, "invalidating obdo/inode %lu\n", inode->i_ino);
501                 /* FIXME: do something better than throwing away everything */
502                 //down(&inode->i_sem);
503                 ll_invalidate_inode_pages(inode);
504                 //up(&inode->i_sem);
505                 break;
506         default:
507                 LBUG();
508         }
509
510         RETURN(0);
511 }
512
513 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
514                             loff_t *ppos)
515 {
516         struct ll_file_data *fd = filp->private_data;
517         struct inode *inode = filp->f_dentry->d_inode;
518         struct ll_sb_info *sbi = ll_i2sbi(inode);
519         struct lustre_handle lockh = { 0, 0 };
520         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
521         int flags = 0;
522         ldlm_error_t err;
523         ssize_t retval;
524         ENTRY;
525
526         CDEBUG(D_VFSTRACE, "VFS Op\n");
527         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
528             !(sbi->ll_flags & LL_SBI_NOLCK)) {
529                 struct ldlm_extent extent;
530                 extent.start = *ppos;
531                 extent.end = *ppos + count - 1;
532                 CDEBUG(D_INFO, "Locking inode %lu, start "LPU64" end "LPU64"\n",
533                        inode->i_ino, extent.start, extent.end);
534
535                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
536                                   &extent, sizeof(extent), LCK_PR, &flags,
537                                   ll_lock_callback, inode, sizeof(*inode),
538                                   &lockh);
539                 if (err != ELDLM_OK) {
540                         CERROR("lock enqueue: err: %d\n", err);
541                         RETURN(err);
542                 }
543         }
544
545         /* If we don't refresh the file size, generic_file_read may not even
546          * call ll_readpage */
547         retval = ll_file_size(inode, lsm, fd->fd_ostdata);
548         if (retval < 0) {
549                 CERROR("ll_file_size: "LPSZ"\n", retval);
550                 RETURN(retval);
551         }
552
553         CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
554                inode->i_ino, count, *ppos);
555         retval = generic_file_read(filp, buf, count, ppos);
556
557         if (retval > 0)
558                 ll_update_atime(inode);
559
560         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
561             !(sbi->ll_flags & LL_SBI_NOLCK)) {
562                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, &lockh);
563                 if (err != ELDLM_OK) {
564                         CERROR("lock cancel: err: %d\n", err);
565                         retval = err;
566                 }
567         }
568
569         RETURN(retval);
570 }
571
572 /*
573  * Write to a file (through the page cache).
574  */
575 static ssize_t
576 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
577 {
578         struct ll_file_data *fd = file->private_data;
579         struct inode *inode = file->f_dentry->d_inode;
580         struct ll_sb_info *sbi = ll_i2sbi(inode);
581         struct lustre_handle lockh = { 0, 0 }, eof_lockh = { 0, 0 };
582         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
583         int flags = 0;
584         ldlm_error_t err;
585         ssize_t retval;
586         ENTRY;
587
588         /* POSIX, but surprised the VFS doesn't check this already */
589         if (count == 0)
590                 return 0;
591
592         CDEBUG(D_VFSTRACE, "VFS Op\n");
593         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
594                 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockh);
595                 if (err)
596                         RETURN(err);
597
598                 /* Get size here so we know extent to enqueue write lock on. */
599                 retval = ll_file_size(inode, lsm, fd->fd_ostdata);
600                 if (retval)
601                         GOTO(out_eof, retval);
602
603                 *ppos = inode->i_size;
604         }
605
606         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
607             !(sbi->ll_flags & LL_SBI_NOLCK)) {
608                 struct ldlm_extent extent;
609                 extent.start = *ppos;
610                 extent.end = *ppos + count - 1;
611                 CDEBUG(D_INFO, "Locking inode %lu, start "LPU64" end "LPU64"\n",
612                        inode->i_ino, extent.start, extent.end);
613
614                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
615                                   &extent, sizeof(extent), LCK_PW, &flags,
616                                   ll_lock_callback, inode, sizeof(*inode),
617                                   &lockh);
618                 if (err != ELDLM_OK) {
619                         CERROR("lock enqueue: err: %d\n", err);
620                         GOTO(out_eof, retval = err);
621                 }
622         }
623
624         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
625                inode->i_ino, count, *ppos);
626
627         retval = generic_file_write(file, buf, count, ppos);
628
629         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
630             !(sbi->ll_flags & LL_SBI_NOLCK)) {
631                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, &lockh);
632                 if (err != ELDLM_OK)
633                         CERROR("lock cancel: err: %d\n", err);
634         }
635
636         EXIT;
637  out_eof:
638         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
639                 err = ll_size_unlock(inode, lsm, LCK_PW, &eof_lockh);
640                 if (err)
641                         CERROR("ll_size_unlock: %d\n", err);
642         }
643
644         return retval;
645 }
646
647 static int ll_lov_setstripe(struct inode *inode, struct file *file,
648                             unsigned long arg)
649 {
650         struct ll_inode_info *lli = ll_i2info(inode);
651         struct lustre_handle *conn = ll_i2obdconn(inode);
652         struct lov_stripe_md *lsm;
653         int rc;
654         ENTRY;
655
656         down(&lli->lli_open_sem);
657         lsm = lli->lli_smd;
658         if (lsm) {
659                 up(&lli->lli_open_sem);
660                 CERROR("stripe already set for ino %lu\n", inode->i_ino);
661                 /* If we haven't already done the open, do so now */
662                 if (file->f_flags & O_LOV_DELAY_CREATE) {
663                         int rc2 = ll_osc_open(conn, inode, file, lsm);
664                         if (rc2)
665                                 RETURN(rc2);
666                 }
667
668                 RETURN(-EALREADY);
669         }
670
671         rc = obd_iocontrol(LL_IOC_LOV_SETSTRIPE, conn, 0, &lsm, (void *)arg);
672         if (rc) {
673                 up(&lli->lli_open_sem);
674                 RETURN(rc);
675         }
676         rc = ll_create_obj(conn, inode, file, lsm);
677         up(&lli->lli_open_sem);
678
679         if (rc) {
680                 obd_free_memmd(conn, &lsm);
681                 RETURN(rc);
682         }
683         rc = ll_osc_open(conn, inode, file, lli->lli_smd);
684         RETURN(rc);
685 }
686
687 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
688 {
689         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
690         struct lustre_handle *conn = ll_i2obdconn(inode);
691
692         if (!lsm)
693                 RETURN(-ENODATA);
694
695         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, conn, 0, lsm, (void *)arg);
696 }
697
698 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
699                   unsigned long arg)
700 {
701         struct ll_file_data *fd = file->private_data;
702         struct lustre_handle *conn;
703         int flags;
704
705         CDEBUG(D_VFSTRACE, "VFS Op\n");
706
707         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* tty ioctls */
708                 return -ENOTTY;
709
710         switch(cmd) {
711         case LL_IOC_GETFLAGS:
712                 /* Get the current value of the file flags */
713                 return put_user(fd->fd_flags, (int *)arg);
714         case LL_IOC_SETFLAGS:
715         case LL_IOC_CLRFLAGS:
716                 /* Set or clear specific file flags */
717                 /* XXX This probably needs checks to ensure the flags are
718                  *     not abused, and to handle any flag side effects.
719                  */
720                 if (get_user(flags, (int *) arg))
721                         return -EFAULT;
722
723                 if (cmd == LL_IOC_SETFLAGS)
724                         fd->fd_flags |= flags;
725                 else
726                         fd->fd_flags &= ~flags;
727                 return 0;
728         case LL_IOC_LOV_SETSTRIPE:
729                 return ll_lov_setstripe(inode, file, arg);
730         case LL_IOC_LOV_GETSTRIPE:
731                 return ll_lov_getstripe(inode, arg);
732
733         /* We need to special case any other ioctls we want to handle,
734          * to send them to the MDS/OST as appropriate and to properly
735          * network encode the arg field.
736         case EXT2_IOC_GETFLAGS:
737         case EXT2_IOC_SETFLAGS:
738         case EXT2_IOC_GETVERSION_OLD:
739         case EXT2_IOC_GETVERSION_NEW:
740         case EXT2_IOC_SETVERSION_OLD:
741         case EXT2_IOC_SETVERSION_NEW:
742         */
743         default:
744                 conn = ll_i2obdconn(inode);
745                 return obd_iocontrol(cmd, conn, 0, NULL, (void *)arg);
746         }
747 }
748
749 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
750 {
751         struct inode *inode = file->f_dentry->d_inode;
752         long long retval;
753         ENTRY;
754
755         CDEBUG(D_VFSTRACE, "VFS Op\n");
756         switch (origin) {
757         case 2: {
758                 struct ll_inode_info *lli = ll_i2info(inode);
759                 struct ll_file_data *fd = file->private_data;
760
761                 retval = ll_file_size(inode, lli->lli_smd, fd->fd_ostdata);
762                 if (retval)
763                         RETURN(retval);
764
765                 offset += inode->i_size;
766                 break;
767         }
768         case 1:
769                 offset += file->f_pos;
770         }
771         retval = -EINVAL;
772         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
773                 if (offset != file->f_pos) {
774                         file->f_pos = offset;
775 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
776                         file->f_reada = 0;
777                         file->f_version = ++event;
778 #endif
779                 }
780                 retval = offset;
781         }
782         RETURN(retval);
783 }
784
785 /* XXX this does not need to do anything for data, it _does_ need to
786    call setattr */
787 int ll_fsync(struct file *file, struct dentry *dentry, int data)
788 {
789         return 0;
790 }
791
792 int ll_inode_revalidate(struct dentry *dentry)
793 {
794         struct inode *inode = dentry->d_inode;
795         struct lov_stripe_md *lsm;
796         ENTRY;
797
798         CDEBUG(D_VFSTRACE, "VFS Op\n");
799         if (!inode) {
800                 CERROR("REPORT THIS LINE TO PETER\n");
801                 RETURN(0);
802         }
803
804         /* this is very tricky.  it is unsafe to call ll_have_md_lock
805            when we have a referenced lock: because it may cause an RPC
806            below when the lock is marked CB_PENDING.  That RPC may not
807            go out because someone else may be in another RPC waiting for
808            that lock*/
809         if (!(dentry->d_it && dentry->d_it->it_lock_mode) &&
810             !ll_have_md_lock(dentry)) {
811                 struct ptlrpc_request *req = NULL;
812                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
813                 struct mds_body *body;
814                 unsigned long valid = 0;
815                 int datalen = 0, rc;
816
817                 /* Why don't we update all valid MDS fields here, if we're
818                  * doing an RPC anyways?  -phil */
819                 if (S_ISREG(inode->i_mode)) {
820                         datalen = obd_size_wiremd(&sbi->ll_osc_conn, NULL);
821                         valid |= OBD_MD_FLEASIZE;
822                 }
823                 rc = mdc_getattr(&sbi->ll_mdc_conn, inode->i_ino,
824                                  inode->i_mode, valid, datalen, &req);
825                 if (rc) {
826                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
827                         ptlrpc_req_finished(req);
828                         RETURN(-abs(rc));
829                 }
830
831                 body = lustre_msg_buf(req->rq_repmsg, 0);
832
833                 if (S_ISREG(inode->i_mode) &&
834                     body->valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) {
835                         CERROR("MDS sent back size for regular file\n");
836                         body->valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
837                 }
838
839                 if (body->valid & OBD_MD_FLEASIZE)
840                         ll_update_inode(inode, body,
841                                         lustre_msg_buf(req->rq_repmsg, 1));
842                 else
843                         ll_update_inode(inode, body, NULL);
844                 ptlrpc_req_finished(req);
845         }
846
847         lsm = ll_i2info(inode)->lli_smd;
848         if (!lsm)       /* object not yet allocated, don't validate size */
849                 RETURN(0);
850
851         /* XXX this should probably become an unconditional obd_getattr()
852          *     so that we update the blocks count and mtime from the OST too.
853          */
854         RETURN(ll_file_size(inode, lsm, NULL));
855 }
856
857 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
858 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
859                       struct kstat *stat)
860 {
861         int res = 0;
862         struct inode *inode = de->d_inode;
863
864         res = ll_inode_revalidate(de);
865         if (res)
866                 return res;
867 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
868         stat->dev = inode->i_dev;
869 #endif
870         stat->ino = inode->i_ino;
871         stat->mode = inode->i_mode;
872         stat->nlink = inode->i_nlink;
873         stat->uid = inode->i_uid;
874         stat->gid = inode->i_gid;
875         stat->rdev = kdev_t_to_nr(inode->i_rdev);
876         stat->atime = inode->i_atime;
877         stat->mtime = inode->i_mtime;
878         stat->ctime = inode->i_ctime;
879         stat->size = inode->i_size;
880         return 0;
881 }
882 #endif
883
884 struct file_operations ll_file_operations = {
885         read:           ll_file_read,
886         write:          ll_file_write,
887         ioctl:          ll_file_ioctl,
888         open:           ll_file_open,
889         release:        ll_file_release,
890         mmap:           generic_file_mmap,
891         llseek:         ll_file_seek,
892         fsync:          NULL
893 };
894
895 struct inode_operations ll_file_inode_operations = {
896         setattr_raw:    ll_setattr_raw,
897         setattr:    ll_setattr,
898         truncate:   ll_truncate,
899 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
900         getattr: ll_getattr,
901 #else
902         revalidate: ll_inode_revalidate,
903 #endif
904 };
905
906 struct inode_operations ll_special_inode_operations = {
907         setattr_raw:    ll_setattr_raw,
908         setattr:    ll_setattr,
909 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
910         getattr:    ll_getattr,
911 #else
912         revalidate: ll_inode_revalidate,
913 #endif
914 };