Whamcloud - gitweb
Branch HEAD
[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 #ifndef AUTOCONF_INCLUDED
25 #include <linux/config.h>
26 #endif
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/smp_lock.h>
33 #include <linux/unistd.h>
34 #include <linux/version.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37
38 #include <linux/fs.h>
39 #include <linux/buffer_head.h>
40 #include <linux/mpage.h>
41 #include <linux/writeback.h>
42 #include <linux/stat.h>
43 #include <asm/uaccess.h>
44 #include <asm/segment.h>
45 #include <linux/mm.h>
46 #include <linux/pagemap.h>
47 #include <linux/smp_lock.h>
48
49 #define DEBUG_SUBSYSTEM S_LLITE
50
51 //#include <lustre_mdc.h>
52 #include <lustre_lite.h>
53 #include "llite_internal.h"
54 #include <linux/lustre_compat25.h>
55
56 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
57 {
58         return ll_writepage(page);
59 }
60
61 /* It is safe to not check anything in invalidatepage/releasepage below
62    because they are run with page locked and all our io is happening with
63    locked page too */
64 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
65 static int ll_invalidatepage(struct page *page, unsigned long offset)
66 {
67         if (offset)
68                 return 0;
69         if (PagePrivate(page))
70                 ll_removepage(page);
71         return 1;
72 }
73 #else
74 static void ll_invalidatepage(struct page *page, unsigned long offset)
75 {
76         if (offset == 0 && PagePrivate(page))
77                 ll_removepage(page);
78 }
79 #endif
80 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
81 {
82         if (PagePrivate(page))
83                 ll_removepage(page);
84         return 1;
85 }
86
87 static int ll_set_page_dirty(struct page *page)
88 {
89         struct ll_async_page *llap;
90         ENTRY;
91         
92         llap = llap_from_page(page, LLAP_ORIGIN_UNKNOWN);
93         if (IS_ERR(llap))
94                 RETURN(PTR_ERR(llap));
95         
96         llap_write_pending(page->mapping->host, llap);
97         RETURN(__set_page_dirty_nobuffers(page));
98 }
99
100 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
101
102 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
103                                     size_t size, struct page ***pages)
104 {
105         int result = -ENOMEM;
106         int page_count;
107
108         /* set an arbitrary limit to prevent arithmetic overflow */
109         if (size > MAX_DIRECTIO_SIZE) {
110                 *pages = NULL;
111                 return -EFBIG;
112         }
113
114         page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
115         page_count -= user_addr >> CFS_PAGE_SHIFT;
116
117         OBD_ALLOC_WAIT(*pages, page_count * sizeof(**pages));
118         if (*pages) {
119                 down_read(&current->mm->mmap_sem);
120                 result = get_user_pages(current, current->mm, user_addr,
121                                         page_count, (rw == READ), 0, *pages,
122                                         NULL);
123                 up_read(&current->mm->mmap_sem);
124                 if (result < 0)
125                         OBD_FREE(*pages, page_count * sizeof(**pages));
126         }
127
128         return result;
129 }
130
131 /*  ll_free_user_pages - tear down page struct array
132  *  @pages: array of page struct pointers underlying target buffer */
133 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
134 {
135         int i;
136
137         for (i = 0; i < npages; i++) {
138                 if (do_dirty)
139                         set_page_dirty_lock(pages[i]);
140                 page_cache_release(pages[i]);
141         }
142
143         OBD_FREE(pages, npages * sizeof(*pages));
144 }
145
146 static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
147                                    struct address_space *mapping,
148                                    struct lov_stripe_md *lsm,
149                                    size_t size, loff_t file_offset,
150                                    struct page **pages, int page_count)
151 {
152         struct brw_page *pga;
153         struct obdo oa;
154         int opc, i, rc = 0;
155         size_t length;
156         struct obd_capa *ocapa;
157         loff_t file_offset_orig = file_offset;
158         ENTRY;
159
160         OBD_ALLOC(pga, sizeof(*pga) * page_count);
161         if (!pga) {
162                 CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
163                        (int)sizeof(*pga), page_count);
164                 RETURN(-ENOMEM);
165         }
166
167         for (i = 0, length = size; length > 0;
168              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
169                 pga[i].pg = pages[i];
170                 pga[i].off = file_offset;
171                 /* To the end of the page, or the length, whatever is less */
172                 pga[i].count = min_t(int, CFS_PAGE_SIZE - 
173                                           (file_offset & ~CFS_PAGE_MASK),
174                                      length);
175                 pga[i].flag = 0;
176                 if (rw == READ)
177                         POISON_PAGE(pages[i], 0x0d);
178         }
179
180         ll_inode_fill_obdo(inode, rw, &oa);
181
182         if (rw == WRITE) {
183                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
184                                     LPROC_LL_DIRECT_WRITE, size);
185                 opc = CAPA_OPC_OSS_WRITE;
186                 llap_write_pending(inode, NULL);
187         } else {
188                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
189                                     LPROC_LL_DIRECT_READ, size);
190                 opc = CAPA_OPC_OSS_RW;
191         }
192         ocapa = ll_osscapa_get(inode, current->fsuid, opc);
193         rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
194                            ll_i2dtexp(inode), &oa, lsm, page_count, pga, NULL,
195                            ocapa);
196         capa_put(ocapa);
197         if ((rc > 0) && (rw == WRITE)) {
198                 lov_stripe_lock(lsm);
199                 obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset_orig + rc, 0);
200                 lov_stripe_unlock(lsm);
201         }
202
203         OBD_FREE(pga, sizeof(*pga) * page_count);
204         RETURN(rc);
205 }
206
207 /* This is the maximum size of a single O_DIRECT request, based on a 128kB
208  * kmalloc limit.  We need to fit all of the brw_page structs, each one
209  * representing PAGE_SIZE worth of user data, into a single buffer, and
210  * then truncate this to be a full-sized RPC.  This is 22MB for 4kB pages. */
211 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
212                       ~(PTLRPC_MAX_BRW_SIZE - 1))
213 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
214                                const struct iovec *iov, loff_t file_offset,
215                                unsigned long nr_segs)
216 {
217         struct file *file = iocb->ki_filp;
218         struct inode *inode = file->f_mapping->host;
219         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
220         struct ll_inode_info *lli = ll_i2info(inode);
221         unsigned long seg = 0;
222         size_t size = MAX_DIO_SIZE;
223         ENTRY;
224
225         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
226                 RETURN(-EBADF);
227
228         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
229         if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
230                 RETURN(-EINVAL);
231
232         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
233                "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
234                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
235                file_offset, file_offset, count >> CFS_PAGE_SHIFT,
236                MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
237
238         if (rw == WRITE)
239                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_WRITE, count);
240         else
241                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_READ, count);
242
243         /* Check that all user buffers are aligned as well */
244         for (seg = 0; seg < nr_segs; seg++) {
245                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
246                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
247                         RETURN(-EINVAL);
248         }
249
250         for (seg = 0; seg < nr_segs; seg++) {
251                 size_t iov_left = iov[seg].iov_len;
252                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
253
254                 while (iov_left > 0) {
255                         struct page **pages;
256                         int page_count;
257                         ssize_t result;
258
259                         page_count = ll_get_user_pages(rw, user_addr,
260                                                        min(size, iov_left),
261                                                        &pages);
262                         LASSERT(page_count != 0);
263                         if (page_count > 0) {
264                                 result = ll_direct_IO_26_seg(rw, inode,
265                                                              file->f_mapping,
266                                                              lli->lli_smd,
267                                                              min(size,iov_left),
268                                                              file_offset, pages,
269                                                              page_count);
270                                 ll_free_user_pages(pages, page_count, rw==READ);
271                         } else {
272                                 result = 0;
273                         }
274                         if (page_count < 0 || result <= 0) {
275                                 /* If we can't allocate a large enough buffer
276                                  * for the request, shrink it to a smaller
277                                  * PAGE_SIZE multiple and try again.
278                                  * We should always be able to kmalloc for a
279                                  * page worth of page pointers = 4MB on i386. */
280                                 if ((page_count == -ENOMEM||result == -ENOMEM)&&
281                                     size > (CFS_PAGE_SIZE / sizeof(*pages)) *
282                                            CFS_PAGE_SIZE) {
283                                         size = ((((size / 2) - 1) |
284                                                  ~CFS_PAGE_MASK) + 1) &
285                                                 CFS_PAGE_MASK;
286                                         CDEBUG(D_VFSTRACE, "DIO size now %u\n",
287                                                (int)size);
288                                         continue;
289                                 }
290
291                                 if (tot_bytes > 0)
292                                         RETURN(tot_bytes);
293                                 RETURN(page_count < 0 ? page_count : result);
294                         }
295
296                         tot_bytes += result;
297                         file_offset += result;
298                         iov_left -= result;
299                         user_addr += result;
300                 }
301         }
302         RETURN(tot_bytes);
303 }
304
305 struct address_space_operations ll_aops = {
306         .readpage       = ll_readpage,
307 //        .readpages      = ll_readpages,
308         .direct_IO      = ll_direct_IO_26,
309         .writepage      = ll_writepage_26,
310         .writepages     = generic_writepages,
311         .set_page_dirty = ll_set_page_dirty,
312         .sync_page      = NULL,
313         .prepare_write  = ll_prepare_write,
314         .commit_write   = ll_commit_write,
315         .invalidatepage = ll_invalidatepage,
316         .releasepage    = ll_releasepage,
317         .bmap           = NULL
318 };