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 = LUSTRE_IT(it)->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, LUSTRE_IT(it)->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 (!LUSTRE_IT(it)->it_disposition)
145 rc = it_open_error(DISP_OPEN_OPEN, it);
147 GOTO(out_release, rc);
149 rc = llu_local_open(lli, it);
153 if (!S_ISREG(lli->lli_st_mode))
154 GOTO(out_release, rc = 0);
156 fd = lli->lli_file_data;
160 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
161 CDEBUG(D_INODE, "object creation was delayed\n");
162 GOTO(out_release, rc);
165 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
167 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
170 request = LUSTRE_IT(it)->it_data;
171 ptlrpc_req_finished(request);
173 it->it_op_release(it);
174 OBD_FREE(it, sizeof(*it));
176 /* libsysio haven't doing anything for O_TRUNC. here we
177 * simply simulate it as open(...); truncate(...);
179 if (rc == 0 && (flags & O_TRUNC) &&
180 S_ISREG(lli->lli_st_mode)) {
183 memset(&attr, 0, sizeof(attr));
185 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
186 rc = llu_setattr_raw(inode, &attr);
188 CERROR("error %d truncate in open()\n", rc);
195 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
197 struct mds_body *body;
198 struct lov_mds_md *eadata;
199 struct lov_stripe_md *lsm = NULL;
200 struct obd_trans_info oti = { 0 };
205 /* req is swabbed so this is safe */
206 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
208 if (!(body->valid & OBD_MD_FLEASIZE))
211 if (body->eadatasize == 0) {
212 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
213 GOTO(out, rc = -EPROTO);
216 /* The MDS sent back the EA because we unlinked the last reference
217 * to this file. Use this EA to unlink the objects on the OST.
218 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
219 * check it is complete and sensible. */
220 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
221 LASSERT(eadata != NULL);
222 if (eadata == NULL) {
223 CERROR("Can't unpack MDS EA data\n");
224 GOTO(out, rc = -EPROTO);
227 rc = obd_unpackmd(llu_i2dtexp(dir), &lsm, eadata, body->eadatasize);
229 CERROR("obd_unpackmd: %d\n", rc);
232 LASSERT(rc >= sizeof(*lsm));
236 GOTO(out_free_memmd, rc = -ENOMEM);
238 oa->o_id = lsm->lsm_object_id;
239 oa->o_mode = body->mode & S_IFMT;
240 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
242 if (body->valid & OBD_MD_FLCOOKIE) {
243 oa->o_valid |= OBD_MD_FLCOOKIE;
245 lustre_msg_buf(request->rq_repmsg, 2,
246 sizeof(struct llog_cookie) *
247 lsm->lsm_stripe_count);
248 if (oti.oti_logcookies == NULL) {
249 oa->o_valid &= ~OBD_MD_FLCOOKIE;
250 body->valid &= ~OBD_MD_FLCOOKIE;
254 rc = obd_destroy(llu_i2dtexp(dir), oa, lsm, &oti);
257 CERROR("obd destroy objid 0x"LPX64" error %d\n",
258 lsm->lsm_object_id, rc);
260 obd_free_memmd(llu_i2dtexp(dir), &lsm);
265 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
267 struct llu_inode_info *lli = llu_i2info(inode);
268 struct ll_file_data *fd = lli->lli_file_data;
269 struct ptlrpc_request *req = NULL;
270 struct obd_client_handle *och = &fd->fd_mds_och;
275 /* clear group lock, if present */
276 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
277 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
278 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
279 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
283 obdo.o_id = lli->lli_st_ino;
284 obdo.o_valid = OBD_MD_FLID;
285 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
286 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
287 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
288 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
290 obdo_from_inode(&obdo, inode, valid);
292 if (0 /* ll_is_inode_dirty(inode) */) {
293 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
294 obdo.o_valid |= OBD_MD_FLFLAGS;
296 rc = mdc_close(mdc_exp, &obdo, och, &req);
298 /* We are the last writer, so the MDS has instructed us to get
299 * the file size and any write cookies, then close again. */
300 //ll_queue_done_writing(inode);
303 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
305 rc = llu_objects_destroy(req, inode);
307 CERROR("inode %lu ll_objects destroy: rc = %d\n",
308 lli->lli_st_ino, rc);
311 mdc_clear_open_replay_data(NULL, och);
312 ptlrpc_req_finished(req);
313 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
314 lli->lli_file_data = NULL;
315 OBD_FREE(fd, sizeof(*fd));
320 int llu_file_release(struct inode *inode)
322 struct ll_file_data *fd;
323 struct llu_sb_info *sbi = llu_i2sbi(inode);
324 struct llu_inode_info *lli = llu_i2info(inode);
328 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
329 lli->lli_st_generation);
331 if (llu_is_root_inode(inode))
334 /* still opened by others? */
335 if (--lli->lli_open_count)
338 fd = lli->lli_file_data;
339 if (!fd) /* no process opened the file after an mcreate */
342 rc2 = llu_mdc_close(sbi->ll_md_exp, inode);
350 * libsysio require us return 0
352 int llu_iop_close(struct inode *inode)
356 liblustre_wait_event(0);
358 rc = llu_file_release(inode);
360 CERROR("file close error %d\n", rc);
362 /* if open count == 0 && stale_flag is set, should we
363 * remove the inode immediately? */
367 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
371 liblustre_wait_event(0);
373 if (off < 0 || off > ll_file_maxbytes(ino))
379 /* this isn't where truncate starts. roughly:
380 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
381 * we grab the lock back in setattr_raw to avoid races. */
382 static void llu_truncate(struct inode *inode)
384 struct llu_inode_info *lli = llu_i2info(inode);
385 struct lov_stripe_md *lsm = lli->lli_smd;
386 struct obdo oa = {0};
389 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
390 lli->lli_st_generation);
393 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
398 oa.o_id = lsm->lsm_object_id;
399 oa.o_valid = OBD_MD_FLID;
400 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
401 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
403 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
404 oa.o_id, lli->lli_st_size);
406 /* truncate == punch from new size to absolute end of file */
407 err = obd_punch(llu_i2dtexp(inode), &oa, lsm, lli->lli_st_size,
408 OBD_OBJECT_EOF, NULL);
410 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
412 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
413 OBD_MD_FLATIME | OBD_MD_FLMTIME |
420 int llu_vmtruncate(struct inode * inode, loff_t offset)
422 struct llu_inode_info *lli = llu_i2info(inode);
424 lli->lli_st_size = offset;