1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light file operations
6 * Copyright (c) 2002-2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
30 #include <sys/types.h>
31 #include <sys/queue.h>
43 #include "llite_lib.h"
45 void llu_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
46 struct inode *i2, const char *name,
47 int namelen, int mode)
51 ll_inode2id(&data->id1, i1);
53 ll_inode2id(&data->id2, i2);
57 data->namelen = namelen;
58 data->create_mode = mode;
59 data->mod_time = CURRENT_TIME;
62 void obdo_refresh_inode(struct inode *dst,
66 struct llu_inode_info *lli = llu_i2info(dst);
67 valid &= src->o_valid;
69 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
70 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
71 src->o_valid, LTIME_S(lli->lli_st_mtime),
72 LTIME_S(lli->lli_st_ctime),
73 (long)src->o_mtime, (long)src->o_ctime);
75 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
76 LTIME_S(lli->lli_st_atime) = src->o_atime;
77 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
78 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
79 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
80 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
81 if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
82 lli->lli_st_size = src->o_size;
84 if (valid & OBD_MD_FLBLKSZ)
85 lli->lli_st_blksize = src->o_blksize;
86 /* allocation of space */
87 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
88 lli->lli_st_blocks = src->o_blocks;
91 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
93 struct ptlrpc_request *req = it->d.lustre.it_data;
94 struct ll_file_data *fd;
95 struct mds_body *body;
98 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
99 LASSERT (body != NULL); /* reply already checked out */
100 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
102 /* already opened? */
103 if (lli->lli_open_count++)
106 LASSERT(!lli->lli_file_data);
108 OBD_ALLOC(fd, sizeof(*fd));
109 /* We can't handle this well without reorganizing ll_file_open and
110 * ll_md_close(), so don't even try right now. */
113 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
114 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
115 lli->lli_file_data = fd;
117 mdc_set_open_replay_data(NULL, &fd->fd_mds_och, it->d.lustre.it_data);
122 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
124 struct inode *inode = pnode->p_base->pb_ino;
125 struct llu_inode_info *lli = llu_i2info(inode);
126 struct ll_file_data *fd;
127 struct ptlrpc_request *request;
128 struct lookup_intent *it;
129 struct lov_stripe_md *lsm;
133 liblustre_wait_event(0);
135 /* don't do anything for '/' */
136 if (llu_is_root_inode(inode))
139 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
140 LL_GET_INTENT(inode, it);
142 if (!it->d.lustre.it_disposition) {
146 rc = it_open_error(DISP_OPEN_OPEN, it);
148 GOTO(out_release, rc);
150 rc = llu_local_open(lli, it);
154 if (!S_ISREG(lli->lli_st_mode))
155 GOTO(out_release, rc = 0);
157 fd = lli->lli_file_data;
161 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
162 CDEBUG(D_INODE, "object creation was delayed\n");
163 GOTO(out_release, rc);
166 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
168 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
171 request = it->d.lustre.it_data;
172 ptlrpc_req_finished(request);
174 it->it_op_release(it);
175 OBD_FREE(it, sizeof(*it));
177 /* libsysio haven't doing anything for O_TRUNC. here we
178 * simply simulate it as open(...); truncate(...);
180 if (rc == 0 && (flags & O_TRUNC) &&
181 S_ISREG(lli->lli_st_mode)) {
184 memset(&attr, 0, sizeof(attr));
186 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
187 rc = llu_setattr_raw(inode, &attr);
189 CERROR("error %d truncate in open()\n", rc);
196 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
198 struct mds_body *body;
199 struct lov_mds_md *eadata;
200 struct lov_stripe_md *lsm = NULL;
201 struct obd_trans_info oti = { 0 };
206 /* req is swabbed so this is safe */
207 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
209 if (!(body->valid & OBD_MD_FLEASIZE))
212 if (body->eadatasize == 0) {
213 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
214 GOTO(out, rc = -EPROTO);
217 /* The MDS sent back the EA because we unlinked the last reference
218 * to this file. Use this EA to unlink the objects on the OST.
219 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
220 * check it is complete and sensible. */
221 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
222 LASSERT(eadata != NULL);
223 if (eadata == NULL) {
224 CERROR("Can't unpack MDS EA data\n");
225 GOTO(out, rc = -EPROTO);
228 rc = obd_unpackmd(llu_i2dtexp(dir), &lsm, eadata, body->eadatasize);
230 CERROR("obd_unpackmd: %d\n", rc);
233 LASSERT(rc >= sizeof(*lsm));
237 GOTO(out_free_memmd, rc = -ENOMEM);
239 oa->o_id = lsm->lsm_object_id;
240 oa->o_mode = body->mode & S_IFMT;
241 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
243 if (body->valid & OBD_MD_FLCOOKIE) {
244 oa->o_valid |= OBD_MD_FLCOOKIE;
246 lustre_msg_buf(request->rq_repmsg, 2,
247 sizeof(struct llog_cookie) *
248 lsm->lsm_stripe_count);
249 if (oti.oti_logcookies == NULL) {
250 oa->o_valid &= ~OBD_MD_FLCOOKIE;
251 body->valid &= ~OBD_MD_FLCOOKIE;
255 rc = obd_destroy(llu_i2dtexp(dir), oa, lsm, &oti);
258 CERROR("obd destroy objid 0x"LPX64" error %d\n",
259 lsm->lsm_object_id, rc);
261 obd_free_memmd(llu_i2dtexp(dir), &lsm);
266 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
268 struct llu_inode_info *lli = llu_i2info(inode);
269 struct ll_file_data *fd = lli->lli_file_data;
270 struct ptlrpc_request *req = NULL;
271 struct obd_client_handle *och = &fd->fd_mds_och;
276 /* clear group lock, if present */
277 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
278 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
279 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
280 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
284 obdo.o_id = lli->lli_st_ino;
285 obdo.o_valid = OBD_MD_FLID;
286 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
287 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
288 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
289 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
291 obdo_from_inode(&obdo, inode, valid);
293 if (0 /* ll_is_inode_dirty(inode) */) {
294 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
295 obdo.o_valid |= OBD_MD_FLFLAGS;
297 rc = mdc_close(mdc_exp, &obdo, och, &req);
299 /* We are the last writer, so the MDS has instructed us to get
300 * the file size and any write cookies, then close again. */
301 //ll_queue_done_writing(inode);
304 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
306 rc = llu_objects_destroy(req, inode);
308 CERROR("inode %lu ll_objects destroy: rc = %d\n",
309 lli->lli_st_ino, rc);
312 mdc_clear_open_replay_data(NULL, och);
313 ptlrpc_req_finished(req);
314 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
315 lli->lli_file_data = NULL;
316 OBD_FREE(fd, sizeof(*fd));
321 int llu_file_release(struct inode *inode)
323 struct ll_file_data *fd;
324 struct llu_sb_info *sbi = llu_i2sbi(inode);
325 struct llu_inode_info *lli = llu_i2info(inode);
329 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
330 lli->lli_st_generation);
332 if (llu_is_root_inode(inode))
335 /* still opened by others? */
336 if (--lli->lli_open_count)
339 fd = lli->lli_file_data;
340 if (!fd) /* no process opened the file after an mcreate */
343 rc2 = llu_mdc_close(sbi->ll_md_exp, inode);
351 * libsysio require us return 0
353 int llu_iop_close(struct inode *inode)
357 liblustre_wait_event(0);
359 rc = llu_file_release(inode);
361 CERROR("file close error %d\n", rc);
363 /* if open count == 0 && stale_flag is set, should we
364 * remove the inode immediately? */
368 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
372 liblustre_wait_event(0);
374 if (off < 0 || off > ll_file_maxbytes(ino))
380 /* this isn't where truncate starts. roughly:
381 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
382 * we grab the lock back in setattr_raw to avoid races. */
383 static void llu_truncate(struct inode *inode)
385 struct llu_inode_info *lli = llu_i2info(inode);
386 struct lov_stripe_md *lsm = lli->lli_smd;
387 struct obdo oa = {0};
390 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
391 lli->lli_st_generation);
394 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
399 oa.o_id = lsm->lsm_object_id;
400 oa.o_valid = OBD_MD_FLID;
401 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
402 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
404 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
405 oa.o_id, lli->lli_st_size);
407 /* truncate == punch from new size to absolute end of file */
408 err = obd_punch(llu_i2dtexp(inode), &oa, lsm, lli->lli_st_size,
409 OBD_OBJECT_EOF, NULL);
411 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
413 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
414 OBD_MD_FLATIME | OBD_MD_FLMTIME |
421 int llu_vmtruncate(struct inode * inode, loff_t offset)
423 struct llu_inode_info *lli = llu_i2info(inode);
425 lli->lli_st_size = offset;