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>
47 #include "llite_lib.h"
49 void llu_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
50 struct inode *i2, const char *name,
51 int namelen, int mode)
55 ll_inode2id(&data->id1, i1);
57 ll_inode2id(&data->id2, i2);
61 data->namelen = namelen;
62 data->create_mode = mode;
63 data->mod_time = CURRENT_TIME;
66 void obdo_refresh_inode(struct inode *dst,
70 struct llu_inode_info *lli = llu_i2info(dst);
71 valid &= src->o_valid;
73 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
74 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
75 src->o_valid, LTIME_S(lli->lli_st_mtime),
76 LTIME_S(lli->lli_st_ctime),
77 (long)src->o_mtime, (long)src->o_ctime);
79 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
80 LTIME_S(lli->lli_st_atime) = src->o_atime;
81 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
82 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
83 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
84 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
85 if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
86 lli->lli_st_size = src->o_size;
88 if (valid & OBD_MD_FLBLKSZ)
89 lli->lli_st_blksize = src->o_blksize;
90 /* allocation of space */
91 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
92 lli->lli_st_blocks = src->o_blocks;
95 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
97 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
98 struct ll_file_data *fd;
99 struct mds_body *body;
102 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
103 LASSERT (body != NULL); /* reply already checked out */
104 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
106 /* already opened? */
107 if (lli->lli_open_count++)
110 LASSERT(!lli->lli_file_data);
112 OBD_ALLOC(fd, sizeof(*fd));
113 /* We can't handle this well without reorganizing ll_file_open and
114 * ll_md_close(), so don't even try right now. */
117 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
118 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
119 lli->lli_file_data = fd;
121 mdc_set_open_replay_data(NULL, &fd->fd_mds_och, LUSTRE_IT(it)->it_data);
126 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
128 struct inode *inode = pnode->p_base->pb_ino;
129 struct llu_inode_info *lli = llu_i2info(inode);
130 struct ll_file_data *fd;
131 struct ptlrpc_request *request;
132 struct lookup_intent *it;
133 struct lov_stripe_md *lsm;
137 liblustre_wait_event(0);
139 /* don't do anything for '/' */
140 if (llu_is_root_inode(inode))
143 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
144 LL_GET_INTENT(inode, it);
146 if (!LUSTRE_IT(it)->it_disposition)
149 rc = it_open_error(DISP_OPEN_OPEN, it);
151 GOTO(out_release, rc);
153 rc = llu_local_open(lli, it);
157 if (!S_ISREG(lli->lli_st_mode))
158 GOTO(out_release, rc = 0);
160 fd = lli->lli_file_data;
164 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
165 CDEBUG(D_INODE, "object creation was delayed\n");
166 GOTO(out_release, rc);
169 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
171 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
174 request = LUSTRE_IT(it)->it_data;
175 ptlrpc_req_finished(request);
177 it->it_op_release(it);
178 OBD_FREE(it, sizeof(*it));
180 /* libsysio haven't doing anything for O_TRUNC. here we
181 * simply simulate it as open(...); truncate(...);
183 if (rc == 0 && (flags & O_TRUNC) &&
184 S_ISREG(lli->lli_st_mode)) {
187 memset(&attr, 0, sizeof(attr));
189 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
190 rc = llu_setattr_raw(inode, &attr);
192 CERROR("error %d truncate in open()\n", rc);
199 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
201 struct mds_body *body;
202 struct lov_mds_md *eadata;
203 struct lov_stripe_md *lsm = NULL;
204 struct obd_trans_info oti = { 0 };
209 /* req is swabbed so this is safe */
210 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
212 if (!(body->valid & OBD_MD_FLEASIZE))
215 if (body->eadatasize == 0) {
216 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
217 GOTO(out, rc = -EPROTO);
220 /* The MDS sent back the EA because we unlinked the last reference
221 * to this file. Use this EA to unlink the objects on the OST.
222 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
223 * check it is complete and sensible. */
224 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
225 LASSERT(eadata != NULL);
226 if (eadata == NULL) {
227 CERROR("Can't unpack MDS EA data\n");
228 GOTO(out, rc = -EPROTO);
231 rc = obd_unpackmd(llu_i2dtexp(dir), &lsm, eadata, body->eadatasize);
233 CERROR("obd_unpackmd: %d\n", rc);
236 LASSERT(rc >= sizeof(*lsm));
240 GOTO(out_free_memmd, rc = -ENOMEM);
242 oa->o_id = lsm->lsm_object_id;
243 oa->o_mode = body->mode & S_IFMT;
244 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
246 if (body->valid & OBD_MD_FLCOOKIE) {
247 oa->o_valid |= OBD_MD_FLCOOKIE;
249 lustre_msg_buf(request->rq_repmsg, 2,
250 sizeof(struct llog_cookie) *
251 lsm->lsm_stripe_count);
252 if (oti.oti_logcookies == NULL) {
253 oa->o_valid &= ~OBD_MD_FLCOOKIE;
254 body->valid &= ~OBD_MD_FLCOOKIE;
258 rc = obd_destroy(llu_i2dtexp(dir), oa, lsm, &oti);
261 CERROR("obd destroy objid 0x"LPX64" error %d\n",
262 lsm->lsm_object_id, rc);
264 obd_free_memmd(llu_i2dtexp(dir), &lsm);
269 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
271 struct llu_inode_info *lli = llu_i2info(inode);
272 struct ll_file_data *fd = lli->lli_file_data;
273 struct ptlrpc_request *req = NULL;
274 struct obd_client_handle *och = &fd->fd_mds_och;
279 /* clear group lock, if present */
280 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
281 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
282 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
283 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
287 obdo.o_id = lli->lli_st_ino;
288 obdo.o_valid = OBD_MD_FLID;
289 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
290 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
291 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
292 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
294 obdo_from_inode(&obdo, inode, valid);
296 if (0 /* ll_is_inode_dirty(inode) */) {
297 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
298 obdo.o_valid |= OBD_MD_FLFLAGS;
300 rc = mdc_close(mdc_exp, &obdo, och, &req);
302 /* We are the last writer, so the MDS has instructed us to get
303 * the file size and any write cookies, then close again. */
304 //ll_queue_done_writing(inode);
307 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
309 rc = llu_objects_destroy(req, inode);
311 CERROR("inode %lu ll_objects destroy: rc = %d\n",
312 lli->lli_st_ino, rc);
315 mdc_clear_open_replay_data(NULL, och);
316 ptlrpc_req_finished(req);
317 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
318 lli->lli_file_data = NULL;
319 OBD_FREE(fd, sizeof(*fd));
324 int llu_file_release(struct inode *inode)
326 struct ll_file_data *fd;
327 struct llu_sb_info *sbi = llu_i2sbi(inode);
328 struct llu_inode_info *lli = llu_i2info(inode);
332 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
333 lli->lli_st_generation);
335 if (llu_is_root_inode(inode))
338 /* still opened by others? */
339 if (--lli->lli_open_count)
342 fd = lli->lli_file_data;
343 if (!fd) /* no process opened the file after an mcreate */
346 rc2 = llu_mdc_close(sbi->ll_md_exp, inode);
354 * libsysio require us return 0
356 int llu_iop_close(struct inode *inode)
360 liblustre_wait_event(0);
362 rc = llu_file_release(inode);
364 CERROR("file close error %d\n", rc);
366 /* if open count == 0 && stale_flag is set, should we
367 * remove the inode immediately? */
371 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
375 liblustre_wait_event(0);
377 if (off < 0 || off > ll_file_maxbytes(ino))
383 /* this isn't where truncate starts. roughly:
384 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
385 * we grab the lock back in setattr_raw to avoid races. */
386 static void llu_truncate(struct inode *inode)
388 struct llu_inode_info *lli = llu_i2info(inode);
389 struct lov_stripe_md *lsm = lli->lli_smd;
390 struct obdo oa = {0};
393 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
394 lli->lli_st_generation);
397 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
402 oa.o_id = lsm->lsm_object_id;
403 oa.o_valid = OBD_MD_FLID;
404 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
405 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
407 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
408 oa.o_id, lli->lli_st_size);
410 /* truncate == punch from new size to absolute end of file */
411 err = obd_punch(llu_i2dtexp(inode), &oa, lsm, lli->lli_st_size,
412 OBD_OBJECT_EOF, NULL);
414 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
416 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
417 OBD_MD_FLATIME | OBD_MD_FLMTIME |
424 int llu_vmtruncate(struct inode * inode, loff_t offset)
426 struct llu_inode_info *lli = llu_i2info(inode);
428 lli->lli_st_size = offset;