Whamcloud - gitweb
22c198a148ea0019b996628591fb3364af9a2d39
[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  *  linux/fs/ext2/file.c
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copyright (C) 1992, 1993, 1994, 1995
10  * Remy Card (card@masi.ibp.fr)
11  * Laboratoire MASI - Institut Blaise Pascal
12  * Universite Pierre et Marie Curie (Paris VI)
13  *
14  *  from
15  *
16  *  linux/fs/minix/file.c
17  *
18  *  Copyright (C) 1991, 1992  Linus Torvalds
19  *
20  *  ext2 fs regular file handling primitives
21  *
22  *  64-bit file support on 64-bit platforms by Jakub Jelinek
23  *      (jj@sunsite.ms.mff.cuni.cz)
24  */
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <linux/lustre_dlm.h>
29 #include <linux/lustre_lite.h>
30
31 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
32 extern int ll_setattr(struct dentry *de, struct iattr *attr);
33
34 static int ll_file_open(struct inode *inode, struct file *file)
35 {
36         int rc;
37         struct ptlrpc_request *req = NULL;
38         struct ll_file_data *fd;
39         struct obdo *oa = NULL;
40         struct lov_stripe_md *md = NULL; 
41         struct ll_sb_info *sbi = ll_i2sbi(inode);
42         struct ll_inode_info *lli = ll_i2info(inode);
43         ENTRY;
44
45         if (file->private_data)
46                 LBUG();
47
48         /*  delayed create of object (intent created inode) */
49         /*  XXX object needs to be cleaned up if mdc_open fails */
50         /*  XXX error handling appropriate here? */
51         if (lli->lli_smd == NULL || lli->lli_smd->lmd_object_id == 0) {
52                 struct client_obd *mdc = sbi2mdc(ll_s2sbi(inode->i_sb));
53                 struct inode * inode = file->f_dentry->d_inode;
54
55                 lli->lli_smd = NULL; 
56                 oa =  obdo_alloc();
57                 if (!oa) { 
58                         RETURN(-ENOMEM);
59                 }
60                 oa->o_mode = S_IFREG | 0600;
61                 oa->o_easize = mdc->cl_max_mdsize;
62                 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE;
63                 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
64                 if (rc)
65                         RETURN(rc);
66                 md = lli->lli_smd;
67                 lli->lli_flags &= ~OBD_FL_CREATEONOPEN;
68         }
69
70         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
71         if (!fd)
72                 GOTO(out, rc = -ENOMEM);
73         memset(fd, 0, sizeof(*fd));
74
75         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
76                       file->f_flags, md,
77                       (__u64)(unsigned long)file, &fd->fd_mdshandle, &req);
78         fd->fd_req = req;
79         ptlrpc_req_finished(req);
80         if (rc)
81                 GOTO(out_req, -abs(rc));
82         if (!fd->fd_mdshandle) {
83                 CERROR("mdc_open didn't assign fd_mdshandle\n");
84                 /* XXX handle this how, abort or is it non-fatal? */
85         }
86         if (!fd->fd_mdshandle)
87                 CERROR("mdc_open didn't assign fd_mdshandle\n");
88
89         if (oa == NULL && (oa = obdo_alloc()) == NULL)
90                 GOTO(out_mdc, rc = -EINVAL);
91
92         oa->o_id = lli->lli_smd->lmd_object_id;
93         oa->o_mode = S_IFREG | inode->i_mode;
94         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID;
95         rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
96         obdo_free(oa);
97         oa = NULL;
98
99         if (rc)
100                 GOTO(out_mdc, rc = -abs(rc));
101
102         file->private_data = fd;
103
104         EXIT;
105
106         return 0;
107 out_mdc:
108         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
109                   S_IFREG, fd->fd_mdshandle, &req);
110 out_req:
111         ptlrpc_free_req(req);
112 //out_fd:
113         obdo_free(oa);
114         kmem_cache_free(ll_file_data_slab, fd);
115         file->private_data = NULL;
116 out:
117         return rc;
118 }
119
120 static int ll_file_release(struct inode *inode, struct file *file)
121 {
122         int rc;
123         struct ptlrpc_request *req = NULL;
124         struct ll_file_data *fd;
125         struct obdo *oa;
126         struct ll_sb_info *sbi = ll_i2sbi(inode);
127         struct ll_inode_info *lli = ll_i2info(inode);
128
129         ENTRY;
130
131         fd = (struct ll_file_data *)file->private_data;
132         if (!fd || !fd->fd_mdshandle) {
133                 LBUG();
134                 GOTO(out, rc = -EINVAL);
135         }
136
137         oa = obdo_alloc();
138         if (oa == NULL) {
139                 LBUG();
140                 GOTO(out_fd, rc = -ENOENT);
141         }
142         oa->o_id = lli->lli_smd->lmd_object_id;
143         oa->o_mode = S_IFREG;
144         oa->o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
145         rc = obd_close(ll_i2obdconn(inode), oa, lli->lli_smd);
146         obdo_free(oa);
147         if (rc)
148                 GOTO(out_fd, abs(rc));
149
150         if (file->f_mode & FMODE_WRITE) {
151                 struct iattr attr;
152                 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
153                 attr.ia_mtime = inode->i_mtime;
154                 attr.ia_ctime = inode->i_ctime;
155                 attr.ia_atime = inode->i_atime;
156                 attr.ia_size = inode->i_size;
157
158                 /* XXX: this introduces a small race that we should evaluate */
159                 rc = ll_inode_setattr(inode, &attr, 0);
160                 if (rc) {
161                         CERROR("failed - %d.\n", rc);
162                         rc = -EIO; /* XXX - GOTO(out)? -phil */
163                 }
164         }
165
166         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
167                        S_IFREG, fd->fd_mdshandle, &req);
168         ptlrpc_req_finished(req);
169         if (rc) {
170                 if (rc > 0)
171                         rc = -rc;
172                 GOTO(out, rc);
173         }
174         ptlrpc_free_req(fd->fd_req);
175
176         EXIT;
177
178 out_fd:
179         kmem_cache_free(ll_file_data_slab, fd);
180         file->private_data = NULL;
181 out:
182         return rc;
183 }
184
185
186 static inline void ll_remove_suid(struct inode *inode)
187 {
188         unsigned int mode;
189
190         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
191         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
192
193         /* was any of the uid bits set? */
194         mode &= inode->i_mode;
195         if (mode && !capable(CAP_FSETID)) {
196                 inode->i_mode &= ~mode;
197                 // XXX careful here - we cannot change the size
198         }
199 }
200
201 static void ll_update_atime(struct inode *inode)
202 {
203         struct iattr attr;
204
205         attr.ia_atime = CURRENT_TIME;
206         attr.ia_valid = ATTR_ATIME;
207
208         if (inode->i_atime == attr.ia_atime) return;
209         if (IS_RDONLY(inode)) return;
210         if (IS_NOATIME(inode)) return;
211
212         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
213         ll_inode_setattr(inode, &attr, 0);
214 }
215
216 static int ll_lock_callback(struct lustre_handle *lockh,
217                             struct ldlm_lock_desc *new,
218                             void *data, __u32 data_len,
219                             struct ptlrpc_request **reqp)
220 {
221         struct inode *inode = data;
222         ENTRY;
223
224         if (new == NULL) {
225                 /* Completion AST.  Do nothing. */
226                 RETURN(0);
227         }
228
229         if (data_len != sizeof(struct inode))
230                 LBUG();
231
232         /* FIXME: do something better than throwing away everything */
233         if (inode == NULL)
234                 LBUG();
235         down(&inode->i_sem);
236         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
237         invalidate_inode_pages(inode);
238         up(&inode->i_sem);
239
240         if (ldlm_cli_cancel(lockh) < 0)
241                 LBUG();
242         RETURN(0);
243 }
244
245 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
246                             loff_t *ppos)
247 {
248         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
249         struct inode *inode = filp->f_dentry->d_inode;
250         struct ll_sb_info *sbi = ll_i2sbi(inode);
251         struct ldlm_extent extent;
252         struct lustre_handle lockh;
253         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
254         int flags = 0;
255         ldlm_error_t err;
256         ssize_t retval;
257         ENTRY;
258
259         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
260                 extent.start = *ppos;
261                 extent.end = *ppos + count;
262                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
263                        inode->i_ino, extent.start, extent.end);
264
265                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
266                                   &extent, sizeof(extent), LCK_PR, &flags,
267                                   ll_lock_callback, inode, sizeof(*inode),
268                                   &lockh);
269                 if (err != ELDLM_OK) {
270                         CERROR("lock enqueue: err: %d\n", err);
271                         RETURN(err);
272                 }
273                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
274         }
275
276         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
277                inode->i_ino, count, *ppos);
278         retval = generic_file_read(filp, buf, count, ppos);
279
280         if (retval > 0)
281                 ll_update_atime(inode);
282
283         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
284                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PR, &lockh);
285                 if (err != ELDLM_OK) {
286                         CERROR("lock cancel: err: %d\n", err);
287                         RETURN(err);
288                 }
289         }
290
291         RETURN(retval);
292 }
293
294 /*
295  * Write to a file (through the page cache).
296  */
297 static ssize_t
298 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
299 {
300         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
301         struct inode *inode = file->f_dentry->d_inode;
302         struct ll_sb_info *sbi = ll_i2sbi(inode);
303         struct ldlm_extent extent;
304         struct lustre_handle lockh;
305         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
306         int flags = 0;
307         ldlm_error_t err;
308         ssize_t retval;
309         ENTRY;
310
311         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
312                 /* FIXME: this should check whether O_APPEND is set and adjust
313                  * extent.start accordingly */
314                 extent.start = *ppos;
315                 extent.end = *ppos + count;
316                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
317                        inode->i_ino, extent.start, extent.end);
318
319                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
320                                   &extent, sizeof(extent), LCK_PW, &flags,
321                                   ll_lock_callback, inode, sizeof(*inode),
322                                   &lockh);
323                 if (err != ELDLM_OK) {
324                         CERROR("lock enqueue: err: %d\n", err);
325                         RETURN(err);
326                 }
327                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
328         }
329
330         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
331                inode->i_ino, (long)count, *ppos);
332
333         retval = generic_file_write(file, buf, count, ppos);
334
335         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
336                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PW, &lockh);
337                 if (err != ELDLM_OK) {
338                         CERROR("lock cancel: err: %d\n", err);
339                         RETURN(err);
340                 }
341         }
342
343         RETURN(retval);
344 }
345
346 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
347                   unsigned long arg)
348 {
349         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
350         int flags;
351
352         switch(cmd) {
353         case LL_IOC_GETFLAGS:
354                 /* Get the current value of the file flags */
355                 return put_user(fd->fd_flags, (int *)arg);
356         case LL_IOC_SETFLAGS:
357         case LL_IOC_CLRFLAGS:
358                 /* Set or clear specific file flags */
359                 /* XXX This probably needs checks to ensure the flags are
360                  *     not abused, and to handle any flag side effects.
361                  */
362                 if (get_user(flags, (int *) arg))
363                         return -EFAULT;
364
365                 if (cmd == LL_IOC_SETFLAGS)
366                         fd->fd_flags |= flags;
367                 else
368                         fd->fd_flags &= ~flags;
369                 return 0;
370
371         /* We need to special case any other ioctls we want to handle,
372          * to send them to the MDS/OST as appropriate and to properly
373          * network encode the arg field.
374         case EXT2_IOC_GETFLAGS:
375         case EXT2_IOC_SETFLAGS:
376         case EXT2_IOC_GETVERSION_OLD:
377         case EXT2_IOC_GETVERSION_NEW:
378         case EXT2_IOC_SETVERSION_OLD:
379         case EXT2_IOC_SETVERSION_NEW:
380         */
381         default:
382                 return -ENOTTY;
383         }
384 }
385
386 /* XXX this does not need to do anything for data, it _does_ need to
387    call setattr */
388 int ll_fsync(struct file *file, struct dentry *dentry, int data)
389 {
390         return 0;
391 }
392
393 struct file_operations ll_file_operations = {
394         read:           ll_file_read,
395         write:          ll_file_write,
396         ioctl:          ll_file_ioctl,
397         open:           ll_file_open,
398         release:        ll_file_release,
399         mmap:           generic_file_mmap,
400         fsync:          NULL
401 };
402
403 struct inode_operations ll_file_inode_operations = {
404         truncate: ll_truncate,
405         setattr: ll_setattr
406 };