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