1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
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)
16 * linux/fs/minix/file.c
18 * Copyright (C) 1991, 1992 Linus Torvalds
20 * ext2 fs regular file handling primitives
22 * 64-bit file support on 64-bit platforms by Jakub Jelinek
23 * (jj@sunsite.ms.mff.cuni.cz)
26 #define DEBUG_SUBSYSTEM S_LLITE
28 #include <linux/lustre_dlm.h>
29 #include <linux/lustre_lite.h>
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);
34 static int ll_file_open(struct inode *inode, struct file *file)
37 struct ptlrpc_request *req = NULL;
38 struct ll_file_data *fd;
39 struct obdo *oa = NULL;
40 struct lov_stripe_md *lsm = NULL;
41 struct ll_sb_info *sbi = ll_i2sbi(inode);
42 struct ll_inode_info *lli = ll_i2info(inode);
45 if (file->private_data)
50 /* delayed create of object (intent created inode) */
51 /* XXX object needs to be cleaned up if mdc_open fails */
52 /* XXX error handling appropriate here? */
54 struct inode *inode = file->f_dentry->d_inode;
56 #warning FIXME: need to get an LDLM lock to prevent racy multi-client creates
57 down(&lli->lli_open_sem);
58 /* Check to see if we lost the race */
59 if (lli->lli_smd == NULL) {
62 up(&lli->lli_open_sem);
65 oa->o_mode = S_IFREG | 0600;
66 oa->o_easize = ll_mds_easize(inode->i_sb);
67 oa->o_id = inode->i_ino;
68 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
70 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
74 up(&lli->lli_open_sem);
78 if (lli->lli_smd->lsm_object_id == 0)
80 up(&lli->lli_open_sem);
84 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
86 GOTO(out, rc = -ENOMEM);
87 memset(fd, 0, sizeof(*fd));
89 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
90 file->f_flags, lsm, (__u64)(unsigned long)file,
91 &fd->fd_mdshandle, &req);
93 ptlrpc_req_finished(req);
95 GOTO(out_req, -abs(rc));
96 if (!fd->fd_mdshandle) {
97 CERROR("mdc_open didn't assign fd_mdshandle\n");
98 /* XXX handle this how, abort or is it non-fatal? */
101 if (oa == NULL && (oa = obdo_alloc()) == NULL)
102 GOTO(out_mdc, rc = -EINVAL);
104 oa->o_id = lsm->lsm_object_id;
105 oa->o_mode = S_IFREG | inode->i_mode;
106 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID | OBD_MD_FLSIZE;
107 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
112 GOTO(out_mdc, rc = -abs(rc));
114 file->private_data = fd;
120 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
121 S_IFREG, fd->fd_mdshandle, &req);
123 ptlrpc_free_req(req);
126 kmem_cache_free(ll_file_data_slab, fd);
127 file->private_data = NULL;
132 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
133 int mode, struct lustre_handle **lockhs_p)
135 struct ll_sb_info *sbi = ll_i2sbi(inode);
136 struct ldlm_extent extent;
137 struct lustre_handle *lockhs = NULL;
140 if (sbi->ll_flags & LL_SBI_NOLCK) {
145 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
149 extent.start = start;
152 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
153 sizeof(extent), mode, &flags, ll_lock_callback,
154 inode, sizeof(*inode), lockhs);
155 if (rc != ELDLM_OK) {
156 CERROR("lock enqueue: %d\n", rc);
157 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
163 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
164 struct lustre_handle *lockhs)
166 struct ll_sb_info *sbi = ll_i2sbi(inode);
169 if (sbi->ll_flags & LL_SBI_NOLCK)
172 if (lockhs == NULL) {
177 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
178 if (rc != ELDLM_OK) {
179 CERROR("lock cancel: %d\n", rc);
183 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
187 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
189 struct ll_sb_info *sbi = ll_i2sbi(inode);
190 struct lustre_handle *lockhs;
194 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
195 if (rc != ELDLM_OK) {
196 CERROR("lock enqueue: %d\n", rc);
200 oa.o_id = lsm->lsm_object_id;
202 oa.o_valid = OBD_MD_FLID|OBD_MD_FLMODE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
203 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
205 obdo_to_inode(inode, &oa, oa.o_valid);
207 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
208 if (err != ELDLM_OK) {
209 CERROR("lock cancel: %d\n", err);
215 static int ll_file_release(struct inode *inode, struct file *file)
218 struct ptlrpc_request *req = NULL;
219 struct ll_file_data *fd;
221 struct ll_sb_info *sbi = ll_i2sbi(inode);
222 struct ll_inode_info *lli = ll_i2info(inode);
226 fd = (struct ll_file_data *)file->private_data;
227 if (!fd || !fd->fd_mdshandle) {
229 GOTO(out, rc = -EINVAL);
232 memset(&oa, 0, sizeof(oa));
233 oa.o_id = lli->lli_smd->lsm_object_id;
235 oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
236 rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
238 GOTO(out_fd, abs(rc));
240 /* If this fails and we goto out_fd, the file size on the MDS is out of
241 * date. Is that a big deal? */
242 if (file->f_mode & FMODE_WRITE) {
243 struct lustre_handle *lockhs;
245 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
247 GOTO(out_fd, abs(rc));
249 oa.o_id = lli->lli_smd->lsm_object_id;
251 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
253 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
256 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
258 attr.ia_mtime = inode->i_mtime;
259 attr.ia_ctime = inode->i_ctime;
260 attr.ia_atime = inode->i_atime;
261 attr.ia_size = oa.o_size;
263 inode->i_blocks = oa.o_blocks;
265 /* XXX: this introduces a small race that we should
267 rc = ll_inode_setattr(inode, &attr, 0);
269 CERROR("failed - %d.\n", rc);
270 rc = -EIO; /* XXX - GOTO(out)? -phil */
273 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
275 CERROR("lock cancel: %d\n", rc);
280 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
281 S_IFREG, fd->fd_mdshandle, &req);
282 ptlrpc_req_finished(req);
288 ptlrpc_req_finished(fd->fd_req);
290 rc = obd_cancel_unused(ll_i2obdconn(inode), lli->lli_smd);
292 CERROR("obd_cancel_unused: %d\n", rc);
297 kmem_cache_free(ll_file_data_slab, fd);
298 file->private_data = NULL;
304 static inline void ll_remove_suid(struct inode *inode)
308 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
309 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
311 /* was any of the uid bits set? */
312 mode &= inode->i_mode;
313 if (mode && !capable(CAP_FSETID)) {
314 inode->i_mode &= ~mode;
315 // XXX careful here - we cannot change the size
319 static void ll_update_atime(struct inode *inode)
323 attr.ia_atime = CURRENT_TIME;
324 attr.ia_valid = ATTR_ATIME;
326 if (inode->i_atime == attr.ia_atime) return;
327 if (IS_RDONLY(inode)) return;
328 if (IS_NOATIME(inode)) return;
330 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
331 ll_inode_setattr(inode, &attr, 0);
334 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
335 void *data, __u32 data_len)
337 struct inode *inode = data;
338 struct lustre_handle lockh;
342 if (data_len != sizeof(struct inode))
348 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
349 /* FIXME: do something better than throwing away everything */
350 invalidate_inode_pages(inode);
353 ldlm_lock2handle(lock, &lockh);
354 rc = ldlm_cli_cancel(&lockh);
356 CERROR("ldlm_cli_cancel failed: %d\n", rc);
360 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
363 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
364 struct inode *inode = filp->f_dentry->d_inode;
365 struct ll_sb_info *sbi = ll_i2sbi(inode);
366 struct ldlm_extent extent;
367 struct lustre_handle *lockhs = NULL;
368 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
374 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
375 sbi->ll_flags & LL_SBI_NOLCK) {
376 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
380 extent.start = *ppos;
381 extent.end = *ppos + count;
382 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
383 inode->i_ino, extent.start, extent.end);
385 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
386 &extent, sizeof(extent), LCK_PR, &flags,
387 ll_lock_callback, inode, sizeof(*inode),
389 if (err != ELDLM_OK) {
390 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
391 CERROR("lock enqueue: err: %d\n", err);
396 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
397 inode->i_ino, count, *ppos);
398 retval = generic_file_read(filp, buf, count, ppos);
401 ll_update_atime(inode);
403 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
404 sbi->ll_flags & LL_SBI_NOLCK) {
405 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
406 if (err != ELDLM_OK) {
407 CERROR("lock cancel: err: %d\n", err);
413 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
418 * Write to a file (through the page cache).
421 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
423 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
424 struct inode *inode = file->f_dentry->d_inode;
425 struct ll_sb_info *sbi = ll_i2sbi(inode);
426 struct ldlm_extent extent;
427 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
428 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
434 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
436 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
440 oa.o_id = lsm->lsm_object_id;
441 oa.o_mode = inode->i_mode;
442 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
444 retval = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
446 GOTO(out_eof, retval);
449 obdo_to_inode(inode, &oa, oa.o_valid);
452 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
453 sbi->ll_flags & LL_SBI_NOLCK) {
454 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
456 GOTO(out_eof, retval = -ENOMEM);
457 extent.start = *ppos;
458 extent.end = *ppos + count;
459 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
460 inode->i_ino, extent.start, extent.end);
462 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
463 &extent, sizeof(extent), LCK_PW, &flags,
464 ll_lock_callback, inode, sizeof(*inode),
466 if (err != ELDLM_OK) {
467 CERROR("lock enqueue: err: %d\n", err);
468 GOTO(out_free, retval = err);
472 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
473 inode->i_ino, (long)count, *ppos);
475 retval = generic_file_write(file, buf, count, ppos);
477 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
478 sbi->ll_flags & LL_SBI_NOLCK) {
479 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
480 if (err != ELDLM_OK) {
481 CERROR("lock cancel: err: %d\n", err);
482 GOTO(out_free, retval = err);
489 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
492 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
493 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
501 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
504 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
508 case LL_IOC_GETFLAGS:
509 /* Get the current value of the file flags */
510 return put_user(fd->fd_flags, (int *)arg);
511 case LL_IOC_SETFLAGS:
512 case LL_IOC_CLRFLAGS:
513 /* Set or clear specific file flags */
514 /* XXX This probably needs checks to ensure the flags are
515 * not abused, and to handle any flag side effects.
517 if (get_user(flags, (int *) arg))
520 if (cmd == LL_IOC_SETFLAGS)
521 fd->fd_flags |= flags;
523 fd->fd_flags &= ~flags;
526 /* We need to special case any other ioctls we want to handle,
527 * to send them to the MDS/OST as appropriate and to properly
528 * network encode the arg field.
529 case EXT2_IOC_GETFLAGS:
530 case EXT2_IOC_SETFLAGS:
531 case EXT2_IOC_GETVERSION_OLD:
532 case EXT2_IOC_GETVERSION_NEW:
533 case EXT2_IOC_SETVERSION_OLD:
534 case EXT2_IOC_SETVERSION_NEW:
541 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
543 struct inode *inode = file->f_dentry->d_inode;
549 struct ll_inode_info *lli = ll_i2info(inode);
551 retval = ll_file_size(inode, lli->lli_smd);
555 offset += inode->i_size;
559 offset += file->f_pos;
562 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
563 if (offset != file->f_pos) {
564 file->f_pos = offset;
566 file->f_version = ++event;
573 /* XXX this does not need to do anything for data, it _does_ need to
575 int ll_fsync(struct file *file, struct dentry *dentry, int data)
580 struct file_operations ll_file_operations = {
582 write: ll_file_write,
583 ioctl: ll_file_ioctl,
585 release: ll_file_release,
586 mmap: generic_file_mmap,
587 llseek: ll_file_seek,
591 struct inode_operations ll_file_inode_operations = {
592 truncate: ll_truncate,