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