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 *md = NULL;
41 struct ll_sb_info *sbi = ll_i2sbi(inode);
42 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_smd == NULL) {
52 struct client_obd *mdc = sbi2mdc(ll_s2sbi(inode->i_sb));
53 struct inode * inode = file->f_dentry->d_inode;
55 down(&lli->lli_open_sem);
56 /* Check to see if we lost the race */
57 if (lli->lli_smd == NULL) {
60 up(&lli->lli_open_sem);
63 oa->o_mode = S_IFREG | 0600;
64 oa->o_easize = mdc->cl_max_mdsize;
65 oa->o_id = inode->i_ino;
66 oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
68 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
72 up(&lli->lli_open_sem);
77 if (lli->lli_smd->lmd_object_id == 0)
79 up(&lli->lli_open_sem);
82 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
84 GOTO(out, rc = -ENOMEM);
85 memset(fd, 0, sizeof(*fd));
87 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
88 file->f_flags, md, (__u64)(unsigned long)file,
89 &fd->fd_mdshandle, &req);
91 ptlrpc_req_finished(req);
93 GOTO(out_req, -abs(rc));
94 if (!fd->fd_mdshandle) {
95 CERROR("mdc_open didn't assign fd_mdshandle\n");
96 /* XXX handle this how, abort or is it non-fatal? */
99 if (oa == NULL && (oa = obdo_alloc()) == NULL)
100 GOTO(out_mdc, rc = -EINVAL);
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 | OBD_MD_FLSIZE;
105 rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
110 GOTO(out_mdc, rc = -abs(rc));
112 file->private_data = fd;
118 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
119 S_IFREG, fd->fd_mdshandle, &req);
121 ptlrpc_free_req(req);
124 kmem_cache_free(ll_file_data_slab, fd);
125 file->private_data = NULL;
130 int ll_size_lock(struct inode *inode, struct lov_stripe_md *md, __u64 start,
131 int mode, struct lustre_handle **lockhs_p)
133 struct ll_sb_info *sbi = ll_i2sbi(inode);
134 struct ldlm_extent extent;
135 struct lustre_handle *lockhs = NULL;
138 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
142 extent.start = start;
145 rc = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT, &extent,
146 sizeof(extent), mode, &flags, ll_lock_callback,
147 inode, sizeof(*inode), lockhs);
148 if (rc != ELDLM_OK) {
149 CERROR("lock enqueue: %d\n", rc);
150 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
156 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *md, int mode,
157 struct lustre_handle *lockhs)
159 struct ll_sb_info *sbi = ll_i2sbi(inode);
162 rc = obd_cancel(&sbi->ll_osc_conn, md, mode, lockhs);
163 if (rc != ELDLM_OK) {
164 CERROR("lock cancel: %d\n", rc);
168 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
172 static int ll_file_release(struct inode *inode, struct file *file)
175 struct ptlrpc_request *req = NULL;
176 struct ll_file_data *fd;
178 struct ll_sb_info *sbi = ll_i2sbi(inode);
179 struct ll_inode_info *lli = ll_i2info(inode);
180 //struct obd_device *obddev = class_conn2obd(&sbi->ll_osc_conn);
181 struct list_head *tmp, *next;
185 fd = (struct ll_file_data *)file->private_data;
186 if (!fd || !fd->fd_mdshandle) {
188 GOTO(out, rc = -EINVAL);
191 memset(&oa, 0, sizeof(oa));
192 oa.o_id = lli->lli_smd->lmd_object_id;
194 oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
195 rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
197 GOTO(out_fd, abs(rc));
199 /* If this fails and we goto out_fd, the file size on the MDS is out of
200 * date. Is that a big deal? */
201 if (file->f_mode & FMODE_WRITE) {
203 struct lustre_handle *lockhs;
205 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
207 GOTO(out_fd, abs(rc));
209 oa.o_id = lli->lli_smd->lmd_object_id;
211 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE;
212 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
214 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
216 attr.ia_mtime = inode->i_mtime;
217 attr.ia_ctime = inode->i_ctime;
218 attr.ia_atime = inode->i_atime;
219 attr.ia_size = oa.o_size;
221 /* XXX: this introduces a small race that we should
223 rc = ll_inode_setattr(inode, &attr, 0);
225 CERROR("failed - %d.\n", rc);
226 rc = -EIO; /* XXX - GOTO(out)? -phil */
229 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
231 CERROR("lock cancel: %d\n", rc);
236 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
237 S_IFREG, fd->fd_mdshandle, &req);
238 ptlrpc_req_finished(req);
244 ptlrpc_free_req(fd->fd_req);
246 // XXX Phil lov devices have no namespace
247 //l_lock(&obddev->obd_namespace->ns_lock);
248 list_for_each_safe(tmp, next, &lli->lli_osc_locks) {
249 struct ldlm_lock *lock;
250 struct lustre_handle lockh;
251 lock = list_entry(tmp, struct ldlm_lock, l_inode_link);
253 if (!list_empty(&lock->l_inode_link)) {
254 list_del_init(&lock->l_inode_link);
257 ldlm_lock2handle(lock, &lockh);
258 rc = ldlm_cli_cancel(&lockh);
260 CERROR("ldlm_cli_cancel: %d\n", rc);
262 //l_unlock(&obddev->obd_namespace->ns_lock);
267 kmem_cache_free(ll_file_data_slab, fd);
268 file->private_data = NULL;
274 static inline void ll_remove_suid(struct inode *inode)
278 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
279 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
281 /* was any of the uid bits set? */
282 mode &= inode->i_mode;
283 if (mode && !capable(CAP_FSETID)) {
284 inode->i_mode &= ~mode;
285 // XXX careful here - we cannot change the size
289 static void ll_update_atime(struct inode *inode)
293 attr.ia_atime = CURRENT_TIME;
294 attr.ia_valid = ATTR_ATIME;
296 if (inode->i_atime == attr.ia_atime) return;
297 if (IS_RDONLY(inode)) return;
298 if (IS_NOATIME(inode)) return;
300 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
301 ll_inode_setattr(inode, &attr, 0);
304 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
305 void *data, __u32 data_len)
307 struct inode *inode = data;
308 struct lustre_handle lockh;
312 if (data_len != sizeof(struct inode))
318 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
319 /* FIXME: do something better than throwing away everything */
320 invalidate_inode_pages(inode);
323 ldlm_lock2handle(lock, &lockh);
324 l_lock(&lock->l_resource->lr_namespace->ns_lock);
325 if (!list_empty(&lock->l_inode_link)) {
326 list_del_init(&lock->l_inode_link);
329 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
331 rc = ldlm_cli_cancel(&lockh);
333 CERROR("ldlm_cli_cancel failed: %d\n", rc);
337 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
340 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
341 struct inode *inode = filp->f_dentry->d_inode;
342 struct ll_sb_info *sbi = ll_i2sbi(inode);
343 struct ldlm_extent extent;
344 struct lustre_handle *lockhs = NULL;
345 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
351 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
352 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
356 extent.start = *ppos;
357 extent.end = *ppos + count;
358 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
359 inode->i_ino, extent.start, extent.end);
361 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
362 &extent, sizeof(extent), LCK_PR, &flags,
363 ll_lock_callback, inode, sizeof(*inode),
365 if (err != ELDLM_OK) {
366 OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
367 CERROR("lock enqueue: err: %d\n", err);
372 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
373 inode->i_ino, count, *ppos);
374 retval = generic_file_read(filp, buf, count, ppos);
377 ll_update_atime(inode);
379 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
380 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PR, lockhs);
381 if (err != ELDLM_OK) {
382 CERROR("lock cancel: err: %d\n", err);
388 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
393 * Write to a file (through the page cache).
396 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
398 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
399 struct inode *inode = file->f_dentry->d_inode;
400 struct ll_sb_info *sbi = ll_i2sbi(inode);
401 struct ldlm_extent extent;
402 struct lustre_handle *lockhs = NULL;
403 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
409 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
410 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
413 /* FIXME: this should check whether O_APPEND is set and adjust
414 * extent.start accordingly */
415 extent.start = *ppos;
416 extent.end = *ppos + count;
417 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
418 inode->i_ino, extent.start, extent.end);
420 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
421 &extent, sizeof(extent), LCK_PW, &flags,
422 ll_lock_callback, inode, sizeof(*inode),
424 if (err != ELDLM_OK) {
425 OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
426 CERROR("lock enqueue: err: %d\n", err);
431 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
432 inode->i_ino, (long)count, *ppos);
434 retval = generic_file_write(file, buf, count, ppos);
436 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
437 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PW, lockhs);
438 if (err != ELDLM_OK) {
439 OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
440 CERROR("lock cancel: err: %d\n", err);
446 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
450 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
453 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
457 case LL_IOC_GETFLAGS:
458 /* Get the current value of the file flags */
459 return put_user(fd->fd_flags, (int *)arg);
460 case LL_IOC_SETFLAGS:
461 case LL_IOC_CLRFLAGS:
462 /* Set or clear specific file flags */
463 /* XXX This probably needs checks to ensure the flags are
464 * not abused, and to handle any flag side effects.
466 if (get_user(flags, (int *) arg))
469 if (cmd == LL_IOC_SETFLAGS)
470 fd->fd_flags |= flags;
472 fd->fd_flags &= ~flags;
475 /* We need to special case any other ioctls we want to handle,
476 * to send them to the MDS/OST as appropriate and to properly
477 * network encode the arg field.
478 case EXT2_IOC_GETFLAGS:
479 case EXT2_IOC_SETFLAGS:
480 case EXT2_IOC_GETVERSION_OLD:
481 case EXT2_IOC_GETVERSION_NEW:
482 case EXT2_IOC_SETVERSION_OLD:
483 case EXT2_IOC_SETVERSION_NEW:
490 /* XXX this does not need to do anything for data, it _does_ need to
492 int ll_fsync(struct file *file, struct dentry *dentry, int data)
497 struct file_operations ll_file_operations = {
499 write: ll_file_write,
500 ioctl: ll_file_ioctl,
502 release: ll_file_release,
503 mmap: generic_file_mmap,
507 struct inode_operations ll_file_inode_operations = {
508 truncate: ll_truncate,