Whamcloud - gitweb
Landing b_smallfix onto HEAD (20040210_1202)
[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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
31 #include <linux/lustre_compat25.h>
32 #endif
33
34 #include "llite_internal.h"
35
36 static int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
37                         struct file *file)
38 {
39         struct ll_file_data *fd = file->private_data;
40         struct ptlrpc_request *req = NULL;
41         struct obd_client_handle *och = &fd->fd_mds_och;
42         struct ll_inode_info *lli = ll_i2info(inode);
43         struct obdo obdo;
44         int rc, valid;
45         ENTRY;
46
47         valid = OBD_MD_FLID;
48         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
49                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
50
51         memset(&obdo, 0, sizeof(obdo));
52         obdo.o_id = inode->i_ino;
53         obdo.o_mode = inode->i_mode;
54         obdo.o_size = inode->i_size;
55         obdo.o_blocks = inode->i_blocks;
56         if (0 /* ll_is_inode_dirty(inode) */) {
57                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
58                 valid |= OBD_MD_FLFLAGS;
59         }
60         obdo.o_valid = valid;
61         rc = mdc_close(mdc_exp, &obdo, och, &req);
62         if (rc == EAGAIN) {
63                 /* We are the last writer, so the MDS has instructed us to get
64                  * the file size and any write cookies, then close again. */
65                 //ll_queue_done_writing(inode);
66                 rc = 0;
67         } else if (rc) {
68                 CERROR("inode %lu mdc close failed: rc = %d\n",
69                        inode->i_ino, rc);
70         }
71         if (rc == 0) {
72                 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
73                 if (rc)
74                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
75                                inode->i_ino, rc);
76         }
77
78         mdc_clear_open_replay_data(och);
79         ptlrpc_req_finished(req);
80         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
81         file->private_data = NULL;
82         OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
83
84         RETURN(rc);
85 }
86
87 /* While this returns an error code, fput() the caller does not, so we need
88  * to make every effort to clean up all of our state here.  Also, applications
89  * rarely check close errors and even if an error is returned they will not
90  * re-try the close call.
91  */
92 int ll_file_release(struct inode *inode, struct file *file)
93 {
94         struct ll_file_data *fd;
95         struct ll_sb_info *sbi = ll_i2sbi(inode);
96         int rc;
97
98         ENTRY;
99         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
100                inode->i_generation, inode);
101
102         /* don't do anything for / */
103         if (inode->i_sb->s_root == file->f_dentry)
104                 RETURN(0);
105
106         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
107         fd = (struct ll_file_data *)file->private_data;
108         LASSERT(fd != NULL);
109
110         rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
111         RETURN(rc);
112 }
113
114 static int ll_intent_file_open(struct file *file, void *lmm,
115                                int lmmsize, struct lookup_intent *itp)
116 {
117         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
118         struct lustre_handle lockh;
119         struct mdc_op_data data;
120         struct dentry *parent = file->f_dentry->d_parent;
121         const char *name = file->f_dentry->d_name.name;
122         const int len = file->f_dentry->d_name.len;
123         int rc;
124
125         if (!parent)
126                 RETURN(-ENOENT);
127
128         ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
129
130         rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, itp, LCK_PR, &data,
131                          &lockh, lmm, lmmsize, ldlm_completion_ast,
132                          ll_mdc_blocking_ast, parent->d_inode);
133         if (rc < 0)
134                 CERROR("lock enqueue: err: %d\n", rc);
135         RETURN(rc);
136 }
137
138 static int ll_local_open(struct file *file, struct lookup_intent *it)
139 {
140         struct ptlrpc_request *req = it->d.lustre.it_data;
141         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
142         struct ll_file_data *fd;
143         struct mds_body *body;
144         ENTRY;
145
146         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
147         LASSERT (body != NULL);                 /* reply already checked out */
148         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
149
150         LASSERT(!file->private_data);
151
152         OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
153         /* We can't handle this well without reorganizing ll_file_open and
154          * ll_mdc_close, so don't even try right now. */
155         LASSERT(fd != NULL);
156
157         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
158         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
159         file->private_data = fd;
160         ll_readahead_init(&fd->fd_ras);
161
162         lli->lli_io_epoch = body->io_epoch;
163
164         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
165
166         RETURN(0);
167 }
168
169 /* Open a file, and (for the very first open) create objects on the OSTs at
170  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
171  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
172  * lli_open_sem to ensure no other process will create objects, send the
173  * stripe MD to the MDS, or try to destroy the objects if that fails.
174  *
175  * If we already have the stripe MD locally then we don't request it in
176  * mdc_open(), by passing a lmm_size = 0.
177  *
178  * It is up to the application to ensure no other processes open this file
179  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
180  * used.  We might be able to avoid races of that sort by getting lli_open_sem
181  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
182  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
183  */
184 int ll_file_open(struct inode *inode, struct file *file)
185 {
186         struct ll_inode_info *lli = ll_i2info(inode);
187         struct lookup_intent *it;
188         struct lov_stripe_md *lsm;
189         struct ptlrpc_request *req;
190         int rc = 0;
191         ENTRY;
192
193         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
194                inode->i_generation, inode);
195
196         /* don't do anything for / */
197         if (inode->i_sb->s_root == file->f_dentry)
198                 RETURN(0);
199
200         it = file->f_it;
201
202         if (!it->d.lustre.it_disposition) {
203                 struct lookup_intent oit = { .it_op = IT_OPEN,
204                                              .it_flags = file->f_flags };
205                 it = &oit;
206                 rc = ll_intent_file_open(file, NULL, 0, it);
207                 if (rc)
208                         GOTO(out, rc);
209         }
210
211         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
212         rc = it_open_error(DISP_OPEN_OPEN, it);
213         if (rc)
214                 GOTO(out, rc);
215
216         rc = ll_local_open(file, it);
217         if (rc)
218                 LBUG();
219
220         if (!S_ISREG(inode->i_mode))
221                 GOTO(out, rc);
222
223         lsm = lli->lli_smd;
224         if (lsm == NULL) {
225                 if (file->f_flags & O_LOV_DELAY_CREATE ||
226                     !(file->f_mode & FMODE_WRITE)) {
227                         CDEBUG(D_INODE, "object creation was delayed\n");
228                         GOTO(out, rc);
229                 }
230         }
231         file->f_flags &= ~O_LOV_DELAY_CREATE;
232         GOTO(out, rc);
233  out:
234         req = it->d.lustre.it_data;
235         ptlrpc_req_finished(req);
236         if (rc == 0)
237                 ll_open_complete(inode);
238         return rc;
239 }
240
241 /* Fills the obdo with the attributes for the inode defined by lsm */
242 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
243                    struct obdo *oa)
244 {
245         struct ptlrpc_request_set *set;
246         int rc;
247         ENTRY;
248
249         LASSERT(lsm != NULL);
250
251         memset(oa, 0, sizeof *oa);
252         oa->o_id = lsm->lsm_object_id;
253         oa->o_mode = S_IFREG;
254         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
255                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
256                 OBD_MD_FLCTIME;
257
258         set = ptlrpc_prep_set();
259         if (set == NULL) {
260                 CERROR ("ENOMEM allocing request set\n");
261                 rc = -ENOMEM;
262         } else {
263                 rc = obd_getattr_async(exp, oa, lsm, set);
264                 if (rc == 0)
265                         rc = ptlrpc_set_wait(set);
266                 ptlrpc_set_destroy(set);
267         }
268         if (rc)
269                 RETURN(rc);
270
271         oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
272                         OBD_MD_FLCTIME | OBD_MD_FLSIZE);
273         RETURN(0);
274 }
275
276 static inline void ll_remove_suid(struct inode *inode)
277 {
278         unsigned int mode;
279
280         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
281         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
282
283         /* was any of the uid bits set? */
284         mode &= inode->i_mode;
285         if (mode && !capable(CAP_FSETID)) {
286                 inode->i_mode &= ~mode;
287                 // XXX careful here - we cannot change the size
288         }
289 }
290
291 /* Flush the page cache for an extent as its canceled.  No one can dirty the
292  * extent until we've finished our work and they can enqueue another lock.
293  * The DLM protects us from ll_file_read/write here, but other kernel actors
294  * could have pages locked */
295 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
296                               struct ldlm_lock *lock)
297 {
298         struct ldlm_extent *extent = &lock->l_policy_data.l_extent;
299         struct obd_export *exp = ll_i2obdexp(inode);
300         struct ll_inode_info *lli = ll_i2info(inode);
301         unsigned long start, end, i;
302         struct page *page;
303         int rc, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
304         ENTRY;
305
306         CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
307                inode->i_ino, inode, extent->start, extent->end, inode->i_size);
308
309         start = extent->start >> PAGE_CACHE_SHIFT;
310         end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
311         if ((end << PAGE_CACHE_SHIFT) < extent->end)
312                 end = ~0;
313
314         i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
315         if (end >= i)
316                 clear_bit(LLI_F_HAVE_OST_SIZE_LOCK,
317                           &(ll_i2info(inode)->lli_flags));
318         if (i < end)
319                 end = i;
320
321         CDEBUG(D_INODE, "walking page indices start: %lu end: %lu\n", start,
322                end);
323
324         for (i = start; i < end; i++) {
325                 ll_pgcache_lock(inode->i_mapping);
326                 if (list_empty(&inode->i_mapping->dirty_pages) &&
327                      list_empty(&inode->i_mapping->clean_pages) &&
328                      list_empty(&inode->i_mapping->locked_pages)) {
329                         CDEBUG(D_INODE, "nothing left\n");
330                         ll_pgcache_unlock(inode->i_mapping);
331                         break;
332                 }
333                 ll_pgcache_unlock(inode->i_mapping);
334
335                 conditional_schedule();
336
337                 page = find_get_page(inode->i_mapping, i);
338                 if (page == NULL)
339                         continue;
340
341                 LL_CDEBUG_PAGE(page, "locking\n");
342                 lock_page(page);
343
344                 /* page->mapping to check with racing against teardown */
345                 if (page->mapping && PageDirty(page) && !discard) {
346                         ClearPageDirty(page);
347                         LL_CDEBUG_PAGE(page, "found dirty\n");
348                         ll_pgcache_lock(inode->i_mapping);
349                         list_del(&page->list);
350                         list_add(&page->list, &inode->i_mapping->locked_pages);
351                         ll_pgcache_unlock(inode->i_mapping);
352
353 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
354                         rc = inode->i_mapping->a_ops->writepage(page);
355 #else
356                         rc = inode->i_mapping->a_ops->writepage(page, NULL);
357 #endif
358                         if (rc != 0) {
359                                 CERROR("writepage of page %p failed: %d\n",
360                                        page, rc);
361                         } else {
362                                 lock_page(page); /* wait for io to complete */
363                         }
364                 }
365
366                 /* checking again to account for writeback's lock_page() */
367                 if (page->mapping != NULL) {
368                         LL_CDEBUG_PAGE(page, "truncating\n");
369 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
370                         truncate_complete_page(page);
371 #else
372                         truncate_complete_page(page->mapping, page);
373 #endif
374                 }
375                 unlock_page(page);
376                 page_cache_release(page);
377         }
378
379         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
380                 rc = obd_lock_contains(exp, lsm, lock, inode->i_size - 1);
381                 if (rc != 0) {
382                         if (rc < 0)
383                                 CERROR("obd_lock_contains: rc = %d\n", rc);
384                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
385                 }
386         }
387
388         EXIT;
389 }
390
391 static int ll_extent_lock_callback(struct ldlm_lock *lock,
392                                    struct ldlm_lock_desc *new, void *data,
393                                    int flag)
394 {
395         struct lustre_handle lockh = { 0 };
396         int rc;
397         ENTRY;
398
399
400         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
401                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
402                 LBUG();
403         }
404
405         switch (flag) {
406         case LDLM_CB_BLOCKING:
407                 ldlm_lock2handle(lock, &lockh);
408                 rc = ldlm_cli_cancel(&lockh);
409                 if (rc != ELDLM_OK)
410                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
411                 break;
412         case LDLM_CB_CANCELING: {
413                 struct inode *inode = ll_inode_from_lock(lock);
414                 struct ll_inode_info *lli;
415
416                 if (!inode)
417                         RETURN(0);
418                 lli= ll_i2info(inode);
419                 if (!lli)
420                         RETURN(0);
421                 if (!lli->lli_smd)
422                         RETURN(0);
423
424                 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
425                 //ll_try_done_writing(inode);
426                 iput(inode);
427                 break;
428         }
429         default:
430                 LBUG();
431         }
432
433         RETURN(0);
434 }
435
436 /*
437  * some callers, notably truncate, really don't want i_size set based
438  * on the the size returned by the getattr, or lock acquisition in
439  * the future.
440  */
441 int ll_extent_lock_no_validate(struct ll_file_data *fd, struct inode *inode,
442                    struct lov_stripe_md *lsm,
443                    int mode, struct ldlm_extent *extent,
444                    struct lustre_handle *lockh, int ast_flags)
445 {
446         struct ll_sb_info *sbi = ll_i2sbi(inode);
447         int rc;
448         ENTRY;
449
450         LASSERT(lockh->cookie == 0);
451
452         /* XXX phil: can we do this?  won't it screw the file size up? */
453         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
454             (sbi->ll_flags & LL_SBI_NOLCK))
455                 RETURN(0);
456
457         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
458                inode->i_ino, extent->start, extent->end);
459
460         rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
461                          sizeof(extent), mode, &ast_flags,
462                          ll_extent_lock_callback, inode, lockh);
463         if (rc > 0)
464                 rc = -EIO;
465         RETURN(rc);
466 }
467
468 /*
469  * this grabs a lock and manually implements behaviour that makes it look like
470  * the OST is returning the file size with each lock acquisition.
471  */
472 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
473                    struct lov_stripe_md *lsm, int mode,
474                    struct ldlm_extent *extent, struct lustre_handle *lockh)
475 {
476         struct ll_inode_info *lli = ll_i2info(inode);
477         struct obd_export *exp = ll_i2obdexp(inode);
478         struct ldlm_extent size_lock;
479         struct lustre_handle match_lockh = {0};
480         struct obdo oa;
481         obd_flag refresh_valid;
482         int flags, rc, matched;
483         ENTRY;
484
485         rc = ll_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
486         if (rc != ELDLM_OK)
487                 RETURN(rc);
488
489         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
490                 RETURN(0);
491
492         rc = ll_lsm_getattr(exp, lsm, &oa);
493         if (rc) 
494                 GOTO(out, rc);
495
496         /* We set this flag in commit write as we extend the file size.  When
497          * the bit is set and the lock is canceled that covers the file size,
498          * we clear the bit.  This is enough to protect the window where our
499          * local size extension is needed for writeback.  However, it relies on
500          * behaviour that won't be true in the near future.  This assumes that
501          * all getattr callers get extent locks, which they currnetly do.  It
502          * also assumes that we only send discarding asts for {0,eof} truncates
503          * as is currently the case.  This will have to be replaced by the
504          * proper eoc communication between clients and the ost, which is on
505          * its way. */
506         refresh_valid = (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME | 
507                          OBD_MD_FLCTIME | OBD_MD_FLSIZE);
508         if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
509                 if (oa.o_size < inode->i_size)
510                         refresh_valid &= ~OBD_MD_FLSIZE;
511                 else 
512                         clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
513         }
514         obdo_refresh_inode(inode, &oa, refresh_valid);
515
516         CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
517                lsm->lsm_object_id, inode->i_size, inode->i_blocks,
518                inode->i_blksize);
519
520         size_lock.start = inode->i_size;
521         size_lock.end = OBD_OBJECT_EOF;
522
523         /* XXX I bet we should be checking the lock ignore flags.. */
524         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
525         matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
526                             sizeof(size_lock), LCK_PR, &flags, inode,
527                             &match_lockh);
528         if (matched < 0)
529                 GOTO(out, rc = matched);
530
531         /* hey, alright, we hold a size lock that covers the size we
532          * just found, its not going to change for a while.. */
533         if (matched == 1) {
534                 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
535                 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
536         }
537
538         rc = 0;
539 out:
540         if (rc)
541                 ll_extent_unlock(fd, inode, lsm, mode, lockh);
542         RETURN(rc);
543 }
544
545 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
546                      struct lov_stripe_md *lsm, int mode,
547                      struct lustre_handle *lockh)
548 {
549         struct ll_sb_info *sbi = ll_i2sbi(inode);
550         int rc;
551         ENTRY;
552
553         /* XXX phil: can we do this?  won't it screw the file size up? */
554         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
555             (sbi->ll_flags & LL_SBI_NOLCK))
556                 RETURN(0);
557
558         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
559
560         RETURN(rc);
561 }
562
563 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
564                             loff_t *ppos)
565 {
566         struct ll_file_data *fd = filp->private_data;
567         struct inode *inode = filp->f_dentry->d_inode;
568         struct ll_inode_info *lli = ll_i2info(inode);
569         struct lov_stripe_md *lsm = lli->lli_smd;
570         struct lustre_handle lockh = { 0 };
571         struct ldlm_extent extent;
572         ldlm_error_t err;
573         ssize_t retval;
574         ENTRY;
575         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
576                inode->i_ino, inode->i_generation, inode, count, *ppos);
577
578         /* "If nbyte is 0, read() will return 0 and have no other results."
579          *                      -- Single Unix Spec */
580         if (count == 0)
581                 RETURN(0);
582
583         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
584                             count);
585
586         if (!lsm)
587                 RETURN(0);
588
589         /* grab a -> eof extent to push extending writes out of node's caches
590          * so we can see them at the getattr after lock acquisition.  this will
591          * turn into a seperate [*ppos + count, EOF] 'size intent' lock attempt
592          * in the future. */
593         extent.start = *ppos;
594         extent.end = OBD_OBJECT_EOF;
595
596         err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
597         if (err != ELDLM_OK)
598                 RETURN(err);
599
600
601         CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
602                inode->i_ino, count, *ppos);
603
604         /* turn off the kernel's read-ahead */
605 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
606         filp->f_ramax = 0;
607 #else
608         filp->f_ra.ra_pages = 0;
609 #endif
610         retval = generic_file_read(filp, buf, count, ppos);
611
612         /* XXX errors? */
613         ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
614         RETURN(retval);
615 }
616
617 /*
618  * Write to a file (through the page cache).
619  */
620 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
621                              loff_t *ppos)
622 {
623         struct ll_file_data *fd = file->private_data;
624         struct inode *inode = file->f_dentry->d_inode;
625         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
626         struct lustre_handle lockh = { 0 };
627         struct ldlm_extent extent;
628         loff_t maxbytes = ll_file_maxbytes(inode);
629         ldlm_error_t err;
630         ssize_t retval;
631         char should_validate = 1;
632         ENTRY;
633         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
634                inode->i_ino, inode->i_generation, inode, count, *ppos);
635
636         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
637
638         /* POSIX, but surprised the VFS doesn't check this already */
639         if (count == 0)
640                 RETURN(0);
641
642         LASSERT(lsm);
643
644         if (file->f_flags & O_APPEND) {
645                 extent.start = 0;
646                 extent.end = OBD_OBJECT_EOF;
647         } else  {
648                 extent.start = *ppos;
649                 extent.end = *ppos + count - 1;
650                 /* we really don't care what i_size is if we're doing
651                  * fully page aligned writes */
652                 if ((*ppos & ~PAGE_CACHE_MASK) == 0 &&
653                     (count & ~PAGE_CACHE_MASK) == 0)
654                         should_validate = 0;
655         }
656
657         if (should_validate)
658                 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
659         else
660                 err = ll_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
661                                                  &extent, &lockh, 0);
662         if (err != ELDLM_OK)
663                 RETURN(err);
664
665         /* this is ok, g_f_w will overwrite this under i_sem if it races
666          * with a local truncate, it just makes our maxbyte checking easier */
667         if (file->f_flags & O_APPEND)
668                 *ppos = inode->i_size;
669
670         if (*ppos >= maxbytes) {
671                 if (count || *ppos > maxbytes) {
672                         send_sig(SIGXFSZ, current, 0);
673                         GOTO(out, retval = -EFBIG);
674                 }
675         }
676         if (*ppos + count > maxbytes)
677                 count = maxbytes - *ppos;
678
679         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
680                inode->i_ino, count, *ppos);
681
682         /* generic_file_write handles O_APPEND after getting i_sem */
683         retval = generic_file_write(file, buf, count, ppos);
684
685 out:
686         /* XXX errors? */
687         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
688                             retval);
689         ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
690         RETURN(retval);
691 }
692
693 static int ll_lov_setstripe(struct inode *inode, struct file *file,
694                             unsigned long arg)
695 {
696         struct ll_inode_info *lli = ll_i2info(inode);
697         struct file *f;
698         struct obd_export *exp = ll_i2obdexp(inode);
699         struct lov_stripe_md *lsm;
700         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = FMODE_WRITE};
701         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
702         struct ptlrpc_request *req = NULL;
703         struct lustre_md md;
704         int rc;
705         ENTRY;
706
707         /* Bug 1152: copy properly when this is no longer true */
708         LASSERT(sizeof(lum) == sizeof(*lump));
709         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
710         rc = copy_from_user(&lum, lump, sizeof(lum));
711         if (rc)
712                 RETURN(-EFAULT);
713
714         down(&lli->lli_open_sem);
715         lsm = lli->lli_smd;
716         if (lsm) {
717                 up(&lli->lli_open_sem);
718                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
719                        inode->i_ino);
720                 RETURN(-EEXIST);
721         }
722
723         f = get_empty_filp();
724         if (!f)
725                 GOTO(out, -ENOMEM);
726
727         f->f_dentry = file->f_dentry;
728         f->f_vfsmnt = file->f_vfsmnt;
729
730         rc = ll_intent_file_open(f, &lum, sizeof(lum), &oit);
731         if (rc)
732                 GOTO(out, rc);
733         if (it_disposition(&oit, DISP_LOOKUP_NEG))
734                 GOTO(out, -ENOENT);
735         req = oit.d.lustre.it_data;
736         rc = oit.d.lustre.it_status;
737
738         if (rc < 0)
739                 GOTO(out, rc);
740
741         rc = mdc_req2lustre_md(req, 1, exp, &md);
742         if (rc)
743                 GOTO(out, rc);
744         ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
745
746         rc = ll_local_open(f, &oit);
747         if (rc)
748                 GOTO(out, rc);
749         ll_intent_release(&oit);
750
751         rc = ll_file_release(f->f_dentry->d_inode, f);
752
753  out:
754         if (f)
755                 put_filp(f);
756         up(&lli->lli_open_sem);
757         if (req != NULL)
758                 ptlrpc_req_finished(req);
759         RETURN(rc);
760 }
761
762 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
763 {
764         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
765
766         if (!lsm)
767                 RETURN(-ENODATA);
768
769         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
770                             (void *)arg);
771 }
772
773 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
774                   unsigned long arg)
775 {
776         struct ll_file_data *fd = file->private_data;
777         int flags;
778         ENTRY;
779
780         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
781                inode->i_generation, inode, cmd);
782
783         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
784                 RETURN(-ENOTTY);
785
786         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
787         switch(cmd) {
788         case LL_IOC_GETFLAGS:
789                 /* Get the current value of the file flags */
790                 return put_user(fd->fd_flags, (int *)arg);
791         case LL_IOC_SETFLAGS:
792         case LL_IOC_CLRFLAGS:
793                 /* Set or clear specific file flags */
794                 /* XXX This probably needs checks to ensure the flags are
795                  *     not abused, and to handle any flag side effects.
796                  */
797                 if (get_user(flags, (int *) arg))
798                         RETURN(-EFAULT);
799
800                 if (cmd == LL_IOC_SETFLAGS)
801                         fd->fd_flags |= flags;
802                 else
803                         fd->fd_flags &= ~flags;
804                 RETURN(0);
805         case LL_IOC_LOV_SETSTRIPE:
806                 RETURN(ll_lov_setstripe(inode, file, arg));
807         case LL_IOC_LOV_GETSTRIPE:
808                 RETURN(ll_lov_getstripe(inode, arg));
809         case EXT3_IOC_GETFLAGS:
810         case EXT3_IOC_SETFLAGS:
811                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
812         /* We need to special case any other ioctls we want to handle,
813          * to send them to the MDS/OST as appropriate and to properly
814          * network encode the arg field.
815         case EXT2_IOC_GETVERSION_OLD:
816         case EXT2_IOC_GETVERSION_NEW:
817         case EXT2_IOC_SETVERSION_OLD:
818         case EXT2_IOC_SETVERSION_NEW:
819         */
820         default:
821                 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
822                                       (void *)arg) );
823         }
824 }
825
826 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
827 {
828         struct inode *inode = file->f_dentry->d_inode;
829         struct ll_file_data *fd = file->private_data;
830         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
831         struct lustre_handle lockh = {0};
832         loff_t retval;
833         ENTRY;
834         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
835                inode->i_generation, inode,
836                offset + ((origin==2) ? inode->i_size : file->f_pos));
837
838         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
839         if (origin == 2) { /* SEEK_END */
840                 ldlm_error_t err;
841                 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
842                 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
843                 if (err != ELDLM_OK)
844                         RETURN(err);
845
846                 offset += inode->i_size;
847         } else if (origin == 1) { /* SEEK_CUR */
848                 offset += file->f_pos;
849         }
850
851         retval = -EINVAL;
852         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
853                 if (offset != file->f_pos) {
854                         file->f_pos = offset;
855 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
856                         file->f_reada = 0;
857                         file->f_version = ++event;
858 #endif
859                 }
860                 retval = offset;
861         }
862
863         if (origin == 2)
864                 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
865         RETURN(retval);
866 }
867
868 int ll_fsync(struct file *file, struct dentry *dentry, int data)
869 {
870         struct inode *inode = dentry->d_inode;
871         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
872         struct ll_fid fid;
873         struct ptlrpc_request *req;
874         int rc, err;
875         ENTRY;
876         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
877                inode->i_generation, inode);
878
879         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
880
881         /* fsync's caller has already called _fdata{sync,write}, we want
882          * that IO to finish before calling the osc and mdc sync methods */
883         rc = filemap_fdatawait(inode->i_mapping);
884
885         ll_inode2fid(&fid, inode);
886         err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
887         if (!rc)
888                 rc = err;
889         if (!err)
890                 ptlrpc_req_finished(req);
891
892         if (data && lsm) {
893                 struct obdo *oa = obdo_alloc();
894
895                 if (!oa)
896                         RETURN(rc ? rc : -ENOMEM);
897
898                 oa->o_id = lsm->lsm_object_id;
899                 oa->o_valid = OBD_MD_FLID;
900                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
901                                            OBD_MD_FLMTIME | OBD_MD_FLCTIME);
902
903                 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
904                                0, OBD_OBJECT_EOF);
905                 if (!rc)
906                         rc = err;
907                 obdo_free(oa);
908         }
909
910         RETURN(rc);
911 }
912
913 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
914 {
915         struct inode *inode = file->f_dentry->d_inode;
916         struct ll_sb_info *sbi = ll_i2sbi(inode);
917         struct obd_device *obddev;
918         struct ldlm_res_id res_id =
919                     { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
920         struct lustre_handle lockh = {0};
921         struct ldlm_flock flock;
922         ldlm_mode_t mode = 0;
923         int flags = 0;
924         int rc;
925         ENTRY;
926
927         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
928                inode->i_ino, file_lock);
929
930         flock.pid = file_lock->fl_pid;
931         flock.start = file_lock->fl_start;
932         flock.end = file_lock->fl_end;
933
934         switch (file_lock->fl_type) {
935         case F_RDLCK:
936                 mode = LCK_PR;
937                 break;
938         case F_UNLCK:
939                 /* An unlock request may or may not have any relation to
940                  * existing locks so we may not be able to pass a lock handle
941                  * via a normal ldlm_lock_cancel() request. The request may even
942                  * unlock a byte range in the middle of an existing lock. In
943                  * order to process an unlock request we need all of the same
944                  * information that is given with a normal read or write record
945                  * lock request. To avoid creating another ldlm unlock (cancel)
946                  * message we'll treat a LCK_NL flock request as an unlock. */
947                 mode = LCK_NL;
948                 break;
949         case F_WRLCK:
950                 mode = LCK_PW;
951                 break;
952         default:
953                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
954                 LBUG();
955         }
956
957         switch (cmd) {
958         case F_SETLKW:
959                 flags = 0;
960                 break;
961         case F_SETLK:
962                 flags = LDLM_FL_BLOCK_NOWAIT;
963                 break;
964         case F_GETLK:
965                 flags = LDLM_FL_TEST_LOCK;
966                 /* Save the old mode so that if the mode in the lock changes we
967                  * can decrement the appropriate reader or writer refcount. */
968                 file_lock->fl_type = mode;
969                 break;
970         default:
971                 CERROR("unknown fcntl lock command: %d\n", cmd);
972                 LBUG();
973         }
974
975         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
976                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.pid,
977                flags, mode, flock.start, flock.end);
978
979         obddev = sbi->ll_mdc_exp->exp_obd;
980         rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
981                               NULL, res_id, LDLM_FLOCK, &flock, sizeof(flock),
982                               mode, &flags, ldlm_flock_completion_ast, NULL,
983                               file_lock, &lockh);
984         RETURN(rc);
985 }
986
987 static int ll_have_md_lock(struct dentry *de)
988 {
989         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
990         struct lustre_handle lockh;
991         struct ldlm_res_id res_id = { .name = {0} };
992         struct obd_device *obddev;
993         int flags;
994         ENTRY;
995
996         if (!de->d_inode)
997                RETURN(0);
998
999         obddev = sbi->ll_mdc_exp->exp_obd;
1000         res_id.name[0] = de->d_inode->i_ino;
1001         res_id.name[1] = de->d_inode->i_generation;
1002
1003         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1004
1005         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1006         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1007                             NULL, 0, LCK_PR, &lockh)) {
1008                 ldlm_lock_decref(&lockh, LCK_PR);
1009                 RETURN(1);
1010         }
1011
1012         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1013                             NULL, 0, LCK_PW, &lockh)) {
1014                 ldlm_lock_decref(&lockh, LCK_PW);
1015                 RETURN(1);
1016         }
1017         RETURN(0);
1018 }
1019
1020 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1021 {
1022         struct inode *inode = dentry->d_inode;
1023         struct lov_stripe_md *lsm;
1024         ENTRY;
1025
1026         if (!inode) {
1027                 CERROR("REPORT THIS LINE TO PETER\n");
1028                 RETURN(0);
1029         }
1030         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1031                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1032 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1033         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1034 #endif
1035
1036         if (!ll_have_md_lock(dentry)) {
1037                 struct ptlrpc_request *req = NULL;
1038                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1039                 struct ll_fid fid;
1040                 unsigned long valid = 0;
1041                 int rc, ealen = 0;
1042
1043                 if (S_ISREG(inode->i_mode)) {
1044                         ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1045                         valid |= OBD_MD_FLEASIZE;
1046                 }
1047                 ll_inode2fid(&fid, inode);
1048                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1049                 if (rc) {
1050                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1051                         RETURN(-abs(rc));
1052                 }
1053                 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1054                 if (rc) {
1055                         ptlrpc_req_finished(req);
1056                         RETURN(rc);
1057                 }
1058                 ptlrpc_req_finished(req);
1059         }
1060
1061 #if 0
1062         if (ll_have_md_lock(dentry) &&
1063             test_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &ll_i2info(inode)->lli_flags))
1064                 RETURN(0);
1065 #endif
1066
1067         lsm = ll_i2info(inode)->lli_smd;
1068         if (!lsm)       /* object not yet allocated, don't validate size */
1069                 RETURN(0);
1070
1071         /* unfortunately stat comes in through revalidate and we don't
1072          * differentiate this use from initial instantiation.  we're
1073          * also being wildly conservative and flushing write caches
1074          * so that stat really returns the proper size. */
1075         {
1076                 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
1077                 struct lustre_handle lockh = {0};
1078                 ldlm_error_t err;
1079
1080                 err = ll_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
1081                 if (err != ELDLM_OK)
1082                         RETURN(err);
1083
1084                 ll_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
1085         }
1086         RETURN(0);
1087 }
1088
1089 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1090 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1091                struct lookup_intent *it, struct kstat *stat)
1092 {
1093         int res = 0;
1094         struct inode *inode = de->d_inode;
1095
1096         res = ll_inode_revalidate_it(de, it);
1097         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1098
1099         if (res)
1100                 return res;
1101
1102         stat->dev = inode->i_sb->s_dev;
1103         stat->ino = inode->i_ino;
1104         stat->mode = inode->i_mode;
1105         stat->nlink = inode->i_nlink;
1106         stat->uid = inode->i_uid;
1107         stat->gid = inode->i_gid;
1108         stat->rdev = kdev_t_to_nr(inode->i_rdev);
1109         stat->atime = inode->i_atime;
1110         stat->mtime = inode->i_mtime;
1111         stat->ctime = inode->i_ctime;
1112         stat->size = inode->i_size;
1113         stat->blksize = inode->i_blksize;
1114         stat->blocks = inode->i_blocks;
1115         return 0;
1116 }
1117 #endif
1118
1119 struct file_operations ll_file_operations = {
1120         read:           ll_file_read,
1121         write:          ll_file_write,
1122         ioctl:          ll_file_ioctl,
1123         open:           ll_file_open,
1124         release:        ll_file_release,
1125         mmap:           generic_file_mmap,
1126         llseek:         ll_file_seek,
1127         fsync:          ll_fsync,
1128         //lock:           ll_file_flock
1129 };
1130
1131 struct inode_operations ll_file_inode_operations = {
1132         setattr_raw:    ll_setattr_raw,
1133         setattr:        ll_setattr,
1134         truncate:       ll_truncate,
1135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1136         getattr_it:     ll_getattr,
1137 #else
1138         revalidate_it:  ll_inode_revalidate_it,
1139 #endif
1140 };
1141
1142 struct inode_operations ll_special_inode_operations = {
1143         setattr_raw:    ll_setattr_raw,
1144         setattr:        ll_setattr,
1145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1146         getattr_it:     ll_getattr,
1147 #else
1148         revalidate_it:  ll_inode_revalidate_it,
1149 #endif
1150 };