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 down(&lli->lli_open_sem);
57 /* Check to see if we lost the race */
58 if (lli->lli_smd == NULL) {
61 up(&lli->lli_open_sem);
64 oa->o_mode = S_IFREG | 0600;
65 oa->o_easize = ll_mds_easize(inode->i_sb);
66 oa->o_id = inode->i_ino;
67 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
69 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
73 up(&lli->lli_open_sem);
77 if (lli->lli_smd->lsm_object_id == 0)
79 up(&lli->lli_open_sem);
83 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
85 GOTO(out, rc = -ENOMEM);
86 memset(fd, 0, sizeof(*fd));
88 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
89 file->f_flags, lsm, (__u64)(unsigned long)file,
90 &fd->fd_mdshandle, &req);
92 ptlrpc_req_finished(req);
94 GOTO(out_req, -abs(rc));
95 if (!fd->fd_mdshandle) {
96 CERROR("mdc_open didn't assign fd_mdshandle\n");
97 /* XXX handle this how, abort or is it non-fatal? */
100 if (oa == NULL && (oa = obdo_alloc()) == NULL)
101 GOTO(out_mdc, rc = -EINVAL);
103 oa->o_id = lsm->lsm_object_id;
104 oa->o_mode = S_IFREG | inode->i_mode;
105 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID | OBD_MD_FLSIZE;
106 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
111 GOTO(out_mdc, rc = -abs(rc));
113 file->private_data = fd;
119 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
120 S_IFREG, fd->fd_mdshandle, &req);
122 ptlrpc_free_req(req);
125 kmem_cache_free(ll_file_data_slab, fd);
126 file->private_data = NULL;
131 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
132 int mode, struct lustre_handle **lockhs_p)
134 struct ll_sb_info *sbi = ll_i2sbi(inode);
135 struct ldlm_extent extent;
136 struct lustre_handle *lockhs = NULL;
137 int rc, flags = 0, stripe_count;
139 if (sbi->ll_flags & LL_SBI_NOLCK) {
144 stripe_count = lsm->lsm_stripe_count;
148 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
152 extent.start = start;
155 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
156 sizeof(extent), mode, &flags, ll_lock_callback,
157 inode, sizeof(*inode), lockhs);
158 if (rc != ELDLM_OK) {
159 CERROR("lock enqueue: %d\n", rc);
160 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
166 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
167 struct lustre_handle *lockhs)
169 struct ll_sb_info *sbi = ll_i2sbi(inode);
170 int rc, stripe_count;
172 if (sbi->ll_flags & LL_SBI_NOLCK)
175 if (lockhs == NULL) {
180 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
181 if (rc != ELDLM_OK) {
182 CERROR("lock cancel: %d\n", rc);
186 stripe_count = lsm->lsm_stripe_count;
190 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
194 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
196 struct ll_sb_info *sbi = ll_i2sbi(inode);
197 struct lustre_handle *lockhs;
201 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
202 if (rc != ELDLM_OK) {
203 CERROR("lock enqueue: %d\n", rc);
207 oa.o_id = lsm->lsm_object_id;
209 oa.o_valid = OBD_MD_FLID|OBD_MD_FLMODE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
210 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
212 obdo_to_inode(inode, &oa, oa.o_valid);
214 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
215 if (err != ELDLM_OK) {
216 CERROR("lock cancel: %d\n", err);
222 static int ll_file_release(struct inode *inode, struct file *file)
225 struct ptlrpc_request *req = NULL;
226 struct ll_file_data *fd;
228 struct ll_sb_info *sbi = ll_i2sbi(inode);
229 struct ll_inode_info *lli = ll_i2info(inode);
233 fd = (struct ll_file_data *)file->private_data;
234 if (!fd || !fd->fd_mdshandle) {
236 GOTO(out, rc = -EINVAL);
239 memset(&oa, 0, sizeof(oa));
240 oa.o_id = lli->lli_smd->lsm_object_id;
242 oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
243 rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
245 GOTO(out_fd, abs(rc));
247 /* If this fails and we goto out_fd, the file size on the MDS is out of
248 * date. Is that a big deal? */
249 if (file->f_mode & FMODE_WRITE) {
250 struct lustre_handle *lockhs;
252 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
254 GOTO(out_fd, abs(rc));
256 oa.o_id = lli->lli_smd->lsm_object_id;
258 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
260 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
263 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
265 attr.ia_mtime = inode->i_mtime;
266 attr.ia_ctime = inode->i_ctime;
267 attr.ia_atime = inode->i_atime;
268 attr.ia_size = oa.o_size;
270 inode->i_blocks = oa.o_blocks;
272 /* XXX: this introduces a small race that we should
274 rc = ll_inode_setattr(inode, &attr, 0);
276 CERROR("failed - %d.\n", rc);
277 rc = -EIO; /* XXX - GOTO(out)? -phil */
280 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
282 CERROR("lock cancel: %d\n", rc);
287 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
288 S_IFREG, fd->fd_mdshandle, &req);
289 ptlrpc_req_finished(req);
295 ptlrpc_req_finished(fd->fd_req);
297 rc = obd_cancel_unused(ll_i2obdconn(inode), lli->lli_smd);
299 CERROR("obd_cancel_unused: %d\n", rc);
304 kmem_cache_free(ll_file_data_slab, fd);
305 file->private_data = NULL;
311 static inline void ll_remove_suid(struct inode *inode)
315 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
316 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
318 /* was any of the uid bits set? */
319 mode &= inode->i_mode;
320 if (mode && !capable(CAP_FSETID)) {
321 inode->i_mode &= ~mode;
322 // XXX careful here - we cannot change the size
326 static void ll_update_atime(struct inode *inode)
330 attr.ia_atime = CURRENT_TIME;
331 attr.ia_valid = ATTR_ATIME;
333 if (inode->i_atime == attr.ia_atime) return;
334 if (IS_RDONLY(inode)) return;
335 if (IS_NOATIME(inode)) return;
337 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
338 ll_inode_setattr(inode, &attr, 0);
341 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
342 void *data, __u32 data_len)
344 struct inode *inode = data;
345 struct lustre_handle lockh;
349 if (data_len != sizeof(struct inode))
355 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
356 /* FIXME: do something better than throwing away everything */
357 invalidate_inode_pages(inode);
360 ldlm_lock2handle(lock, &lockh);
361 rc = ldlm_cli_cancel(&lockh);
363 CERROR("ldlm_cli_cancel failed: %d\n", rc);
367 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
370 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
371 struct inode *inode = filp->f_dentry->d_inode;
372 struct ll_sb_info *sbi = ll_i2sbi(inode);
373 struct ldlm_extent extent;
374 struct lustre_handle *lockhs = NULL;
375 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
381 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
382 sbi->ll_flags & LL_SBI_NOLCK) {
383 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
387 extent.start = *ppos;
388 extent.end = *ppos + count;
389 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
390 inode->i_ino, extent.start, extent.end);
392 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
393 &extent, sizeof(extent), LCK_PR, &flags,
394 ll_lock_callback, inode, sizeof(*inode),
396 if (err != ELDLM_OK) {
397 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
398 CERROR("lock enqueue: err: %d\n", err);
403 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
404 inode->i_ino, count, *ppos);
405 retval = generic_file_read(filp, buf, count, ppos);
408 ll_update_atime(inode);
410 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
411 sbi->ll_flags & LL_SBI_NOLCK) {
412 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
413 if (err != ELDLM_OK) {
414 CERROR("lock cancel: err: %d\n", err);
420 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
425 * Write to a file (through the page cache).
428 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
430 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
431 struct inode *inode = file->f_dentry->d_inode;
432 struct ll_sb_info *sbi = ll_i2sbi(inode);
433 struct ldlm_extent extent;
434 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
435 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
441 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
443 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
447 oa.o_id = lsm->lsm_object_id;
448 oa.o_mode = inode->i_mode;
449 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
451 retval = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
453 GOTO(out_eof, retval);
456 obdo_to_inode(inode, &oa, oa.o_valid);
459 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
460 sbi->ll_flags & LL_SBI_NOLCK) {
461 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
463 GOTO(out_eof, retval = -ENOMEM);
464 extent.start = *ppos;
465 extent.end = *ppos + count;
466 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
467 inode->i_ino, extent.start, extent.end);
469 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
470 &extent, sizeof(extent), LCK_PW, &flags,
471 ll_lock_callback, inode, sizeof(*inode),
473 if (err != ELDLM_OK) {
474 CERROR("lock enqueue: err: %d\n", err);
475 GOTO(out_free, retval = err);
479 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
480 inode->i_ino, (long)count, *ppos);
482 retval = generic_file_write(file, buf, count, ppos);
484 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
485 sbi->ll_flags & LL_SBI_NOLCK) {
486 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
487 if (err != ELDLM_OK) {
488 CERROR("lock cancel: err: %d\n", err);
489 GOTO(out_free, retval = err);
496 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
499 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
500 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
508 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
511 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
515 case LL_IOC_GETFLAGS:
516 /* Get the current value of the file flags */
517 return put_user(fd->fd_flags, (int *)arg);
518 case LL_IOC_SETFLAGS:
519 case LL_IOC_CLRFLAGS:
520 /* Set or clear specific file flags */
521 /* XXX This probably needs checks to ensure the flags are
522 * not abused, and to handle any flag side effects.
524 if (get_user(flags, (int *) arg))
527 if (cmd == LL_IOC_SETFLAGS)
528 fd->fd_flags |= flags;
530 fd->fd_flags &= ~flags;
533 /* We need to special case any other ioctls we want to handle,
534 * to send them to the MDS/OST as appropriate and to properly
535 * network encode the arg field.
536 case EXT2_IOC_GETFLAGS:
537 case EXT2_IOC_SETFLAGS:
538 case EXT2_IOC_GETVERSION_OLD:
539 case EXT2_IOC_GETVERSION_NEW:
540 case EXT2_IOC_SETVERSION_OLD:
541 case EXT2_IOC_SETVERSION_NEW:
548 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
550 struct inode *inode = file->f_dentry->d_inode;
556 struct ll_inode_info *lli = ll_i2info(inode);
558 retval = ll_file_size(inode, lli->lli_smd);
562 offset += inode->i_size;
566 offset += file->f_pos;
569 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
570 if (offset != file->f_pos) {
571 file->f_pos = offset;
573 file->f_version = ++event;
580 /* XXX this does not need to do anything for data, it _does_ need to
582 int ll_fsync(struct file *file, struct dentry *dentry, int data)
587 struct file_operations ll_file_operations = {
589 write: ll_file_write,
590 ioctl: ll_file_ioctl,
592 release: ll_file_release,
593 mmap: generic_file_mmap,
594 llseek: ll_file_seek,
598 struct inode_operations ll_file_inode_operations = {
599 truncate: ll_truncate,