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;
40 struct ll_sb_info *sbi = ll_i2sbi(inode);
41 struct ll_inode_info *lli = ll_i2info(inode);
45 if (file->private_data)
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_obdo == NULL) {
52 struct inode * inode = file->f_dentry->d_inode;
54 oa = lli->lli_obdo = obdo_alloc();
55 oa->o_valid = OBD_MD_FLMODE;
56 oa->o_mode = S_IFREG | 0600;
57 rc = obd_create(ll_i2obdconn(inode), oa);
60 lli->lli_flags &= ~OBD_FL_CREATEONOPEN;
66 GOTO(out_mdc, rc = -EINVAL);
69 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
71 GOTO(out, rc = -ENOMEM);
72 memset(fd, 0, sizeof(*fd));
74 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG, file->f_flags,
75 id, (__u64)(unsigned long)file, &fd->fd_mdshandle, &req);
77 ptlrpc_req_finished(req);
79 GOTO(out_req, -abs(rc));
80 if (!fd->fd_mdshandle) {
81 CERROR("mdc_open didn't assign fd_mdshandle\n");
82 /* XXX handle this how, abort or is it non-fatal? */
84 if (!fd->fd_mdshandle)
85 CERROR("mdc_open didn't assign fd_mdshandle\n");
87 rc = obd_open(ll_i2obdconn(inode), oa);
89 GOTO(out_mdc, rc = -abs(rc));
91 file->private_data = fd;
97 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
98 S_IFREG, fd->fd_mdshandle, &req);
100 ptlrpc_free_req(req);
102 kmem_cache_free(ll_file_data_slab, fd);
103 file->private_data = NULL;
108 static int ll_file_release(struct inode *inode, struct file *file)
111 struct ptlrpc_request *req = NULL;
112 struct ll_file_data *fd;
114 struct ll_sb_info *sbi = ll_i2sbi(inode);
118 fd = (struct ll_file_data *)file->private_data;
119 if (!fd || !fd->fd_mdshandle) {
121 GOTO(out, rc = -EINVAL);
124 oa = ll_i2info(inode)->lli_obdo;
127 GOTO(out_fd, rc = -ENOENT);
129 rc = obd_close(ll_i2obdconn(inode), oa);
131 GOTO(out_fd, abs(rc));
133 if (file->f_mode & FMODE_WRITE) {
135 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
136 attr.ia_mtime = inode->i_mtime;
137 attr.ia_ctime = inode->i_ctime;
138 attr.ia_atime = inode->i_atime;
139 attr.ia_size = inode->i_size;
141 /* XXX: this introduces a small race that we should evaluate */
142 rc = ll_inode_setattr(inode, &attr, 0);
144 CERROR("failed - %d.\n", rc);
145 rc = -EIO; /* XXX - GOTO(out)? -phil */
149 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
150 S_IFREG, fd->fd_mdshandle, &req);
151 ptlrpc_req_finished(req);
157 ptlrpc_free_req(fd->fd_req);
162 kmem_cache_free(ll_file_data_slab, fd);
163 file->private_data = NULL;
169 static inline void ll_remove_suid(struct inode *inode)
173 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
174 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
176 /* was any of the uid bits set? */
177 mode &= inode->i_mode;
178 if (mode && !capable(CAP_FSETID)) {
179 inode->i_mode &= ~mode;
180 // XXX careful here - we cannot change the size
184 static void ll_update_atime(struct inode *inode)
188 attr.ia_atime = CURRENT_TIME;
189 attr.ia_valid = ATTR_ATIME;
191 if (inode->i_atime == attr.ia_atime) return;
192 if (IS_RDONLY(inode)) return;
193 if (IS_NOATIME(inode)) return;
195 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
196 ll_inode_setattr(inode, &attr, 0);
199 static int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
200 void *data, __u32 data_len)
202 struct inode *inode = lock->l_data;
206 /* Completion AST. Do nothing. */
210 if (data_len != sizeof(struct inode))
213 /* FIXME: do something better than throwing away everything */
217 invalidate_inode_pages(inode);
220 if (ldlm_cli_cancel(lock->l_client, lock) < 0)
225 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
228 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
229 struct inode *inode = filp->f_dentry->d_inode;
230 struct ll_sb_info *sbi = ll_i2sbi(inode);
231 struct ldlm_extent extent;
232 struct lustre_handle lockh;
233 __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
239 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
240 extent.start = *ppos;
241 extent.end = *ppos + count;
242 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
243 inode->i_ino, extent.start, extent.end);
245 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
246 &extent, sizeof(extent), LCK_PR, &flags,
247 ll_lock_callback, inode, sizeof(*inode),
250 CERROR("lock enqueue: err: %d\n", err);
251 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
254 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
255 inode->i_ino, count, *ppos);
256 retval = generic_file_read(filp, buf, count, ppos);
259 ll_update_atime(inode);
261 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
262 err = obd_cancel(&sbi->ll_osc_conn, LCK_PR, &lockh);
264 CERROR("lock cancel: err: %d\n", err);
271 * Write to a file (through the page cache).
274 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
276 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
277 struct inode *inode = file->f_dentry->d_inode;
278 struct ll_sb_info *sbi = ll_i2sbi(inode);
279 struct ldlm_extent extent;
280 struct lustre_handle lockh;
281 __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
287 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
288 extent.start = *ppos;
289 extent.end = *ppos + count;
290 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
291 inode->i_ino, extent.start, extent.end);
293 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
294 &extent, sizeof(extent), LCK_PW, &flags,
295 ll_lock_callback, inode, sizeof(*inode),
298 CERROR("lock enqueue: err: %d\n", err);
299 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
302 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
303 inode->i_ino, (long)count, *ppos);
305 retval = generic_file_write(file, buf, count, ppos);
307 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
308 err = obd_cancel(&sbi->ll_osc_conn, LCK_PW, &lockh);
310 CERROR("lock cancel: err: %d\n", err);
316 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
319 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
323 case LL_IOC_GETFLAGS:
324 /* Get the current value of the file flags */
325 return put_user(fd->fd_flags, (int *)arg);
326 case LL_IOC_SETFLAGS:
327 case LL_IOC_CLRFLAGS:
328 /* Set or clear specific file flags */
329 /* XXX This probably needs checks to ensure the flags are
330 * not abused, and to handle any flag side effects.
332 if (get_user(flags, (int *) arg))
335 if (cmd == LL_IOC_SETFLAGS)
336 fd->fd_flags |= flags;
338 fd->fd_flags &= ~flags;
341 /* We need to special case any other ioctls we want to handle,
342 * to send them to the MDS/OST as appropriate and to properly
343 * network encode the arg field.
344 case EXT2_IOC_GETFLAGS:
345 case EXT2_IOC_SETFLAGS:
346 case EXT2_IOC_GETVERSION_OLD:
347 case EXT2_IOC_GETVERSION_NEW:
348 case EXT2_IOC_SETVERSION_OLD:
349 case EXT2_IOC_SETVERSION_NEW:
356 /* XXX this does not need to do anything for data, it _does_ need to
358 int ll_fsync(struct file *file, struct dentry *dentry, int data)
363 struct file_operations ll_file_operations = {
365 write: ll_file_write,
366 ioctl: ll_file_ioctl,
368 release: ll_file_release,
369 mmap: generic_file_mmap,
373 struct inode_operations ll_file_inode_operations = {
374 truncate: ll_truncate,