Whamcloud - gitweb
ll_revalid_it will pack fid2, while its capability might expire yet,
[fs/lustre-release.git] / lustre / llite / rw26.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/fs.h>
37 #include <linux/buffer_head.h>
38 #include <linux/mpage.h>
39 #include <linux/writeback.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/mm.h>
44 #include <linux/pagemap.h>
45 #include <linux/smp_lock.h>
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48
49 #include <lustre_mdc.h>
50 #include <lustre_lite.h>
51 #include "llite_internal.h"
52 #include <linux/lustre_compat25.h>
53
54 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
55 {
56         return ll_writepage(page);
57 }
58
59 /* It is safe to not check anything in invalidatepage/releasepage below
60    because they are run with page locked and all our io is happening with
61    locked page too */
62 static int ll_invalidatepage(struct page *page, unsigned long offset)
63 {
64         if (offset)
65                 return 0;
66         if (PagePrivate(page))
67                 ll_removepage(page);
68         return 1;
69 }
70
71 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
72 {
73         if (PagePrivate(page))
74                 ll_removepage(page);
75         return 1;
76 }
77
78 static int ll_set_page_dirty(struct page *page)
79 {
80         struct ll_async_page *llap;
81         ENTRY;
82         
83         llap = llap_from_page(page, LLAP_ORIGIN_UNKNOWN);
84         if (IS_ERR(llap))
85                 RETURN(PTR_ERR(llap));
86         
87         llap_write_pending(page->mapping->host, llap);
88         RETURN(__set_page_dirty_nobuffers(page));
89 }
90
91 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
92
93 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
94                                     size_t size, struct page ***pages)
95 {
96         int result = -ENOMEM;
97         unsigned long page_count;
98
99         /* set an arbitrary limit to prevent arithmetic overflow */
100         if (size > MAX_DIRECTIO_SIZE) {
101                 *pages = NULL;
102                 return -EFBIG;
103         }
104
105         page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
106         page_count -= user_addr >> PAGE_SHIFT;
107
108         OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
109         if (*pages) {
110                 down_read(&current->mm->mmap_sem);
111                 result = get_user_pages(current, current->mm, user_addr,
112                                         page_count, (rw == READ), 0, *pages,
113                                         NULL);
114                 up_read(&current->mm->mmap_sem);
115         }
116
117         return result;
118 }
119
120 /*  ll_free_user_pages - tear down page struct array
121  *  @pages: array of page struct pointers underlying target buffer */
122 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
123 {
124         int i;
125
126         for (i = 0; i < npages; i++) {
127                 if (do_dirty)
128                         set_page_dirty_lock(pages[i]);
129                 page_cache_release(pages[i]);
130         }
131
132         OBD_FREE(pages, npages * sizeof(*pages));
133 }
134
135 static ssize_t ll_direct_IO_26_seg(int rw, struct file *file,
136                                    struct address_space *mapping,
137                                    struct inode *inode,
138                                    struct lov_stripe_md *lsm,
139                                    unsigned long user_addr, size_t size,
140                                    loff_t file_offset, struct page **pages,
141                                    int page_count)
142 {
143         struct brw_page *pga;
144         struct obdo oa;
145         int opc, i, rc = 0;
146         size_t length;
147         struct obd_capa *ocapa;
148         ENTRY;
149
150         OBD_ALLOC(pga, sizeof(*pga) * page_count);
151         if (!pga)
152                 RETURN(-ENOMEM);
153
154         for (i = 0, length = size; length > 0;
155              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
156                 pga[i].pg = pages[i];
157                 pga[i].off = file_offset;
158                 /* To the end of the page, or the length, whatever is less */
159                 pga[i].count = min_t(int, PAGE_SIZE -(file_offset & ~PAGE_MASK),
160                                      length);
161                 pga[i].flag = 0;
162                 if (rw == READ)
163                         POISON_PAGE(pages[i], 0x0d);
164         }
165
166         ll_inode_fill_obdo(inode, rw, &oa);
167
168         if (rw == WRITE) {
169                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
170                                     LPROC_LL_DIRECT_WRITE, size);
171                 opc = CAPA_OPC_OSS_WRITE;
172                 llap_write_pending(inode, NULL);
173         } else {
174                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
175                                     LPROC_LL_DIRECT_READ, size);
176                 opc = CAPA_OPC_OSS_READ | CAPA_OPC_OSS_WRITE;
177         }
178         ocapa = ll_osscapa_get(inode, opc);
179         rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
180                            ll_i2dtexp(inode), &oa, lsm, page_count, pga, NULL,
181                            ocapa);
182         capa_put(ocapa);
183         if (rc == 0) {
184                 rc = size;
185                 if (rw == WRITE) {
186                         lov_stripe_lock(lsm);
187                         obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
188                         lov_stripe_unlock(lsm);
189                 }
190         }
191
192         OBD_FREE(pga, sizeof(*pga) * page_count);
193         RETURN(rc);
194 }
195
196 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
197                                const struct iovec *iov, loff_t file_offset,
198                                unsigned long nr_segs)
199 {
200         struct file *file = iocb->ki_filp;
201         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
202         struct ll_inode_info *lli = ll_i2info(file->f_mapping->host);
203         unsigned long seg = 0;
204         ENTRY;
205
206         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
207                 RETURN(-EBADF);
208
209         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
210         if ((file_offset & (PAGE_SIZE - 1)) || (count & (PAGE_SIZE - 1)))
211                 RETURN(-EINVAL);
212
213         while ((seg < nr_segs) && (tot_bytes >= 0)) {
214                 const struct iovec *vec = &iov[seg++];
215                 unsigned long user_addr = (unsigned long)vec->iov_base;
216                 size_t size = vec->iov_len;
217                 struct page **pages;
218                 int page_count;
219                 ssize_t result;
220
221                 page_count = ll_get_user_pages(rw, user_addr, size, &pages);
222                 if (page_count < 0) {
223                         ll_free_user_pages(pages, 0, 0);
224                         if (tot_bytes > 0)
225                                 break;
226                         return page_count;
227                 }
228
229                 result = ll_direct_IO_26_seg(rw, file, file->f_mapping,
230                                              file->f_mapping->host,
231                                              lli->lli_smd, user_addr, size,
232                                              file_offset, pages, page_count);
233                 ll_free_user_pages(pages, page_count, rw == READ);
234
235                 if (result <= 0) {
236                         if (tot_bytes > 0)
237                                 break;
238                         return result;
239                 }
240
241                 tot_bytes += result;
242                 file_offset += result;
243                 if (result < size)
244                         break;
245         }
246         return tot_bytes;
247 }
248
249 struct address_space_operations ll_aops = {
250         .readpage       = ll_readpage,
251 //        .readpages      = ll_readpages,
252         .direct_IO      = ll_direct_IO_26,
253         .writepage      = ll_writepage_26,
254         .writepages     = generic_writepages,
255         .set_page_dirty = ll_set_page_dirty,
256         .sync_page      = NULL,
257         .prepare_write  = ll_prepare_write,
258         .commit_write   = ll_commit_write,
259         .invalidatepage = ll_invalidatepage,
260         .releasepage    = ll_releasepage,
261         .bmap           = NULL
262 };