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_op_data(struct mdc_op_data *data,
54 ll_i2uctxt(&data->ctxt, i1, i2);
55 ll_inode2fid(&data->fid1, i1);
58 ll_inode2fid(&data->fid2, i2);
62 data->namelen = namelen;
63 data->create_mode = mode;
64 data->mod_time = CURRENT_TIME;
67 void obdo_refresh_inode(struct inode *dst,
71 struct llu_inode_info *lli = llu_i2info(dst);
72 valid &= src->o_valid;
74 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
75 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
76 src->o_valid, LTIME_S(lli->lli_st_mtime),
77 LTIME_S(lli->lli_st_ctime),
78 (long)src->o_mtime, (long)src->o_ctime);
80 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
81 LTIME_S(lli->lli_st_atime) = src->o_atime;
82 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
83 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
84 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
85 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
86 if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
87 lli->lli_st_size = src->o_size;
89 if (valid & OBD_MD_FLBLKSZ)
90 lli->lli_st_blksize = src->o_blksize;
91 /* allocation of space */
92 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
93 lli->lli_st_blocks = src->o_blocks;
96 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
98 struct ptlrpc_request *req = it->d.lustre.it_data;
99 struct ll_file_data *fd;
100 struct mds_body *body;
103 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
104 LASSERT (body != NULL); /* reply already checked out */
105 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
107 /* already opened? */
108 if (lli->lli_open_count++)
111 LASSERT(!lli->lli_file_data);
113 OBD_ALLOC(fd, sizeof(*fd));
114 /* We can't handle this well without reorganizing ll_file_open and
115 * ll_mdc_close, so don't even try right now. */
118 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
119 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
120 lli->lli_file_data = fd;
122 mdc_set_open_replay_data(NULL, &fd->fd_mds_och, it->d.lustre.it_data);
127 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
129 struct inode *inode = pnode->p_base->pb_ino;
130 struct llu_inode_info *lli = llu_i2info(inode);
131 struct ll_file_data *fd;
132 struct ptlrpc_request *request;
133 struct lookup_intent *it;
134 struct lov_stripe_md *lsm;
138 liblustre_wait_event(0);
140 /* don't do anything for '/' */
141 if (llu_is_root_inode(inode))
144 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
145 LL_GET_INTENT(inode, it);
147 if (!it->d.lustre.it_disposition) {
151 rc = it_open_error(DISP_OPEN_OPEN, it);
153 GOTO(out_release, rc);
155 rc = llu_local_open(lli, it);
159 if (!S_ISREG(lli->lli_st_mode))
160 GOTO(out_release, rc = 0);
162 fd = lli->lli_file_data;
166 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
167 CDEBUG(D_INODE, "object creation was delayed\n");
168 GOTO(out_release, rc);
171 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
173 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
176 request = it->d.lustre.it_data;
177 ptlrpc_req_finished(request);
179 it->it_op_release(it);
180 OBD_FREE(it, sizeof(*it));
182 /* libsysio haven't doing anything for O_TRUNC. here we
183 * simply simulate it as open(...); truncate(...);
185 if (rc == 0 && (flags & O_TRUNC) &&
186 S_ISREG(lli->lli_st_mode)) {
189 memset(&attr, 0, sizeof(attr));
191 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
192 rc = llu_setattr_raw(inode, &attr);
194 CERROR("error %d truncate in open()\n", rc);
201 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
203 struct mds_body *body;
204 struct lov_mds_md *eadata;
205 struct lov_stripe_md *lsm = NULL;
206 struct obd_trans_info oti = { 0 };
211 /* req is swabbed so this is safe */
212 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
214 if (!(body->valid & OBD_MD_FLEASIZE))
217 if (body->eadatasize == 0) {
218 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
219 GOTO(out, rc = -EPROTO);
222 /* The MDS sent back the EA because we unlinked the last reference
223 * to this file. Use this EA to unlink the objects on the OST.
224 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
225 * check it is complete and sensible. */
226 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
227 LASSERT(eadata != NULL);
228 if (eadata == NULL) {
229 CERROR("Can't unpack MDS EA data\n");
230 GOTO(out, rc = -EPROTO);
233 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
235 CERROR("obd_unpackmd: %d\n", rc);
238 LASSERT(rc >= sizeof(*lsm));
242 GOTO(out_free_memmd, rc = -ENOMEM);
244 oa->o_id = lsm->lsm_object_id;
245 oa->o_mode = body->mode & S_IFMT;
246 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
248 if (body->valid & OBD_MD_FLCOOKIE) {
249 oa->o_valid |= OBD_MD_FLCOOKIE;
251 lustre_msg_buf(request->rq_repmsg, 2,
252 sizeof(struct llog_cookie) *
253 lsm->lsm_stripe_count);
254 if (oti.oti_logcookies == NULL) {
255 oa->o_valid &= ~OBD_MD_FLCOOKIE;
256 body->valid &= ~OBD_MD_FLCOOKIE;
260 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
263 CERROR("obd destroy objid 0x"LPX64" error %d\n",
264 lsm->lsm_object_id, rc);
266 obd_free_memmd(llu_i2obdexp(dir), &lsm);
271 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
273 struct llu_inode_info *lli = llu_i2info(inode);
274 struct ll_file_data *fd = lli->lli_file_data;
275 struct ptlrpc_request *req = NULL;
276 struct obd_client_handle *och = &fd->fd_mds_och;
281 /* clear group lock, if present */
282 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
283 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
284 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
285 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
289 obdo.o_id = lli->lli_st_ino;
290 obdo.o_valid = OBD_MD_FLID;
291 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
292 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
293 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
294 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
296 obdo_from_inode(&obdo, inode, valid);
298 if (0 /* ll_is_inode_dirty(inode) */) {
299 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
300 obdo.o_valid |= OBD_MD_FLFLAGS;
302 rc = mdc_close(mdc_exp, &obdo, och, &req);
304 /* We are the last writer, so the MDS has instructed us to get
305 * the file size and any write cookies, then close again. */
306 //ll_queue_done_writing(inode);
309 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
311 rc = llu_objects_destroy(req, inode);
313 CERROR("inode %lu ll_objects destroy: rc = %d\n",
314 lli->lli_st_ino, rc);
317 mdc_clear_open_replay_data(NULL, och);
318 ptlrpc_req_finished(req);
319 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
320 lli->lli_file_data = NULL;
321 OBD_FREE(fd, sizeof(*fd));
326 int llu_file_release(struct inode *inode)
328 struct ll_file_data *fd;
329 struct llu_sb_info *sbi = llu_i2sbi(inode);
330 struct llu_inode_info *lli = llu_i2info(inode);
334 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
335 lli->lli_st_generation);
337 if (llu_is_root_inode(inode))
340 /* still opened by others? */
341 if (--lli->lli_open_count)
344 fd = lli->lli_file_data;
345 if (!fd) /* no process opened the file after an mcreate */
348 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
356 * libsysio require us return 0
358 int llu_iop_close(struct inode *inode)
362 liblustre_wait_event(0);
364 rc = llu_file_release(inode);
366 CERROR("file close error %d\n", rc);
368 /* if open count == 0 && stale_flag is set, should we
369 * remove the inode immediately? */
373 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
377 liblustre_wait_event(0);
379 if (off < 0 || off > ll_file_maxbytes(ino))
385 /* this isn't where truncate starts. roughly:
386 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
387 * we grab the lock back in setattr_raw to avoid races. */
388 static void llu_truncate(struct inode *inode)
390 struct llu_inode_info *lli = llu_i2info(inode);
391 struct lov_stripe_md *lsm = lli->lli_smd;
392 struct obdo oa = {0};
395 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
396 lli->lli_st_generation);
399 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
404 oa.o_id = lsm->lsm_object_id;
405 oa.o_valid = OBD_MD_FLID;
406 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
407 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
409 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
410 oa.o_id, lli->lli_st_size);
412 /* truncate == punch from new size to absolute end of file */
413 err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
414 OBD_OBJECT_EOF, NULL);
416 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
418 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
419 OBD_MD_FLATIME | OBD_MD_FLMTIME |
426 int llu_vmtruncate(struct inode * inode, loff_t offset)
428 struct llu_inode_info *lli = llu_i2info(inode);
430 lli->lli_st_size = offset;