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