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);
33 extern inline struct obdo * ll_oa_from_inode(struct inode *inode,
36 static int ll_file_open(struct inode *inode, struct file *file)
39 struct ptlrpc_request *req = NULL;
40 struct ll_file_data *fd;
42 struct ll_sb_info *sbi = ll_i2sbi(inode);
45 if (file->private_data)
48 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
50 GOTO(out, rc = -ENOMEM);
51 memset(fd, 0, sizeof(*fd));
53 rc = mdc_open(&sbi->ll_mds_client, sbi->ll_mds_conn, inode->i_ino,
54 S_IFREG, file->f_flags, (__u64)(unsigned long)file,
55 &fd->fd_mdshandle, &req);
57 ptlrpc_req_finished(req);
63 if (!fd->fd_mdshandle)
64 CERROR("mdc_open didn't assign fd_mdshandle\n");
67 oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
70 rc = obd_open(ll_i2obdconn(inode), oa);
73 /* XXX: Need to do mdc_close here! */
74 GOTO(out, rc = abs(rc));
77 file->private_data = fd;
82 kmem_cache_free(ll_file_data_slab, fd);
83 file->private_data = NULL;
89 static int ll_file_release(struct inode *inode, struct file *file)
92 struct ptlrpc_request *req = NULL;
93 struct ll_file_data *fd;
95 struct ll_sb_info *sbi = ll_i2sbi(inode);
99 fd = (struct ll_file_data *)file->private_data;
100 if (!fd || !fd->fd_mdshandle) {
102 GOTO(out, rc = -EINVAL);
105 oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
108 rc = obd_close(ll_i2obdconn(inode), oa);
113 if (file->f_flags & O_WRONLY) {
115 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
116 attr.ia_mtime = inode->i_mtime;
117 attr.ia_ctime = inode->i_ctime;
118 attr.ia_atime = inode->i_atime;
119 attr.ia_size = inode->i_size;
121 /* XXX: this introduces a small race that we should evaluate */
122 rc = ll_inode_setattr(inode, &attr, 0);
124 CERROR("failed - %d.\n", rc);
125 rc = -EIO; /* XXX - GOTO(out)? -phil */
129 rc = mdc_close(&sbi->ll_mds_client, sbi->ll_mds_conn, inode->i_ino,
130 S_IFREG, fd->fd_mdshandle, &req);
131 ptlrpc_req_finished(req);
137 ptlrpc_free_req(fd->fd_req);
143 kmem_cache_free(ll_file_data_slab, fd);
144 file->private_data = NULL;
150 static inline void ll_remove_suid(struct inode *inode)
154 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
155 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
157 /* was any of the uid bits set? */
158 mode &= inode->i_mode;
159 if (mode && !capable(CAP_FSETID)) {
160 inode->i_mode &= ~mode;
161 // XXX careful here - we cannot change the size
165 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
168 struct inode *inode = filp->f_dentry->d_inode;
169 struct ll_sb_info *sbi = ll_i2sbi(inode);
170 struct ldlm_extent extent;
171 struct ldlm_handle lockh;
172 __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
178 extent.start = *ppos;
179 extent.end = *ppos + count;
180 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
181 inode->i_ino, extent.start, extent.end);
183 err = obd_enqueue(&sbi->ll_conn, sbi->ll_namespace, NULL, res_id,
184 LDLM_EXTENT, &extent, LCK_PR, &flags, inode,
185 sizeof(*inode), &lockh);
187 CERROR("lock enqueue: err: %d\n", err);
188 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
190 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
191 inode->i_ino, count, *ppos);
192 retval = generic_file_read(filp, buf, count, ppos);
195 attr.ia_valid = ATTR_ATIME;
196 attr.ia_atime = CURRENT_TIME;
197 ll_setattr(filp->f_dentry, &attr);
200 err = obd_cancel(&sbi->ll_conn, LCK_PR, &lockh);
202 CERROR("lock cancel: err: %d\n", err);
208 * Write to a file (through the page cache).
211 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
213 struct inode *inode = file->f_dentry->d_inode;
214 struct ll_sb_info *sbi = ll_i2sbi(inode);
215 struct ldlm_extent extent;
216 struct ldlm_handle lockh;
217 __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
223 extent.start = *ppos;
224 extent.end = *ppos + count;
225 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
226 inode->i_ino, extent.start, extent.end);
228 err = obd_enqueue(&sbi->ll_conn, sbi->ll_namespace, NULL, res_id,
229 LDLM_EXTENT, &extent, LCK_PW, &flags, inode,
230 sizeof(*inode), &lockh);
232 CERROR("lock enqueue: err: %d\n", err);
233 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
235 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
236 inode->i_ino, (long)count, *ppos);
238 retval = generic_file_write(file, buf, count, ppos);
240 err = obd_cancel(&sbi->ll_conn, LCK_PW, &lockh);
242 CERROR("lock cancel: err: %d\n", err);
247 /* XXX this does not need to do anything for data, it _does_ need to
249 int ll_fsync(struct file *file, struct dentry *dentry, int data)
254 struct file_operations ll_file_operations = {
256 write: ll_file_write,
258 release: ll_file_release,
259 mmap: generic_file_mmap,
263 struct inode_operations ll_file_inode_operations = {
264 truncate: ll_truncate,