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, 2003 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>
41 #include "llite_lib.h"
43 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
52 ll_i2uctxt(&data->ctxt, i1, i2);
53 ll_inode2fid(&data->fid1, i1);
56 ll_inode2fid(&data->fid2, i2);
60 data->namelen = namelen;
61 data->create_mode = mode;
62 data->mod_time = CURRENT_TIME;
65 void obdo_refresh_inode(struct inode *dst,
69 struct llu_inode_info *lli = llu_i2info(dst);
70 valid &= src->o_valid;
72 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
73 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
74 src->o_valid, LTIME_S(lli->lli_st_mtime),
75 LTIME_S(lli->lli_st_ctime),
76 (long)src->o_mtime, (long)src->o_ctime);
78 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
79 LTIME_S(lli->lli_st_atime) = src->o_atime;
80 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
81 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
82 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
83 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
84 if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
85 lli->lli_st_size = src->o_size;
87 if (valid & OBD_MD_FLBLKSZ)
88 lli->lli_st_blksize = src->o_blksize;
89 /* allocation of space */
90 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
91 lli->lli_st_blocks = src->o_blocks;
94 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
96 struct ptlrpc_request *req = it->d.lustre.it_data;
97 struct ll_file_data *fd;
98 struct mds_body *body;
101 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
102 LASSERT (body != NULL); /* reply already checked out */
103 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
105 /* already opened? */
106 if (lli->lli_open_count++)
109 LASSERT(!lli->lli_file_data);
111 OBD_ALLOC(fd, sizeof(*fd));
112 /* We can't handle this well without reorganizing ll_file_open and
113 * ll_mdc_close, so don't even try right now. */
116 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
117 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
118 lli->lli_file_data = fd;
120 mdc_set_open_replay_data(NULL, &fd->fd_mds_och, it->d.lustre.it_data);
125 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
127 struct inode *inode = pnode->p_base->pb_ino;
128 struct llu_inode_info *lli = llu_i2info(inode);
129 struct ll_file_data *fd;
130 struct ptlrpc_request *request;
131 struct lookup_intent *it;
132 struct lov_stripe_md *lsm;
136 /* don't do anything for '/' */
137 if (llu_is_root_inode(inode))
140 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
141 LL_GET_INTENT(inode, it);
143 if (!it->d.lustre.it_disposition) {
147 rc = it_open_error(DISP_OPEN_OPEN, it);
149 GOTO(out_release, rc);
151 rc = llu_local_open(lli, it);
155 if (!S_ISREG(lli->lli_st_mode))
156 GOTO(out_release, rc = 0);
158 fd = lli->lli_file_data;
162 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
163 CDEBUG(D_INODE, "object creation was delayed\n");
164 GOTO(out_release, rc);
167 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
169 lli->lli_open_flags = flags;
172 request = it->d.lustre.it_data;
173 ptlrpc_req_finished(request);
175 it->it_op_release(it);
176 OBD_FREE(it, sizeof(*it));
181 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
183 struct mds_body *body;
184 struct lov_mds_md *eadata;
185 struct lov_stripe_md *lsm = NULL;
186 struct obd_trans_info oti = { 0 };
191 /* req is swabbed so this is safe */
192 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
194 if (!(body->valid & OBD_MD_FLEASIZE))
197 if (body->eadatasize == 0) {
198 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
199 GOTO(out, rc = -EPROTO);
202 /* The MDS sent back the EA because we unlinked the last reference
203 * to this file. Use this EA to unlink the objects on the OST.
204 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
205 * check it is complete and sensible. */
206 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
207 LASSERT(eadata != NULL);
208 if (eadata == NULL) {
209 CERROR("Can't unpack MDS EA data\n");
210 GOTO(out, rc = -EPROTO);
213 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
215 CERROR("obd_unpackmd: %d\n", rc);
218 LASSERT(rc >= sizeof(*lsm));
222 GOTO(out_free_memmd, rc = -ENOMEM);
224 oa->o_id = lsm->lsm_object_id;
225 oa->o_mode = body->mode & S_IFMT;
226 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
228 if (body->valid & OBD_MD_FLCOOKIE) {
229 oa->o_valid |= OBD_MD_FLCOOKIE;
231 lustre_msg_buf(request->rq_repmsg, 2,
232 sizeof(struct llog_cookie) *
233 lsm->lsm_stripe_count);
234 if (oti.oti_logcookies == NULL) {
235 oa->o_valid &= ~OBD_MD_FLCOOKIE;
236 body->valid &= ~OBD_MD_FLCOOKIE;
240 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
243 CERROR("obd destroy objid 0x"LPX64" error %d\n",
244 lsm->lsm_object_id, rc);
246 obd_free_memmd(llu_i2obdexp(dir), &lsm);
251 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
253 struct llu_inode_info *lli = llu_i2info(inode);
254 struct ll_file_data *fd = lli->lli_file_data;
255 struct ptlrpc_request *req = NULL;
256 struct obd_client_handle *och = &fd->fd_mds_och;
261 /* clear group lock, if present */
262 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
263 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
264 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
265 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
269 obdo.o_id = lli->lli_st_ino;
270 obdo.o_valid = OBD_MD_FLID;
271 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
272 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
273 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
274 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
276 obdo_from_inode(&obdo, inode, valid);
278 if (0 /* ll_is_inode_dirty(inode) */) {
279 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
280 obdo.o_valid |= OBD_MD_FLFLAGS;
282 rc = mdc_close(mdc_exp, &obdo, och, &req);
284 /* We are the last writer, so the MDS has instructed us to get
285 * the file size and any write cookies, then close again. */
286 //ll_queue_done_writing(inode);
289 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
291 rc = llu_objects_destroy(req, inode);
293 CERROR("inode %lu ll_objects destroy: rc = %d\n",
294 lli->lli_st_ino, rc);
297 mdc_clear_open_replay_data(NULL, och);
298 ptlrpc_req_finished(req);
299 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
300 lli->lli_file_data = NULL;
301 OBD_FREE(fd, sizeof(*fd));
306 int llu_file_release(struct inode *inode)
308 struct ll_file_data *fd;
309 struct llu_sb_info *sbi = llu_i2sbi(inode);
310 struct llu_inode_info *lli = llu_i2info(inode);
314 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
315 lli->lli_st_generation);
317 if (llu_is_root_inode(inode))
320 /* still opened by others? */
321 if (--lli->lli_open_count)
324 fd = lli->lli_file_data;
325 if (!fd) /* no process opened the file after an mcreate */
328 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
335 int llu_iop_close(struct inode *inode)
339 rc = llu_file_release(inode);
340 /* if open count == 0 && stale_flag is set, should we
341 * remove the inode immediately? */
345 int llu_iop_ipreadv(struct inode *ino,
350 if (!ioctx->ioctx_iovlen)
352 if (ioctx->ioctx_iovlen < 0)
355 ioctx->ioctx_private = llu_file_read(ino,
358 ioctx->ioctx_offset);
359 if (IS_ERR(ioctx->ioctx_private))
360 return (PTR_ERR(ioctx->ioctx_private));
365 int llu_iop_ipwritev(struct inode *ino,
370 if (!ioctx->ioctx_iovlen)
372 if (ioctx->ioctx_iovlen < 0)
375 ioctx->ioctx_private = llu_file_write(ino,
378 ioctx->ioctx_offset);
379 if (IS_ERR(ioctx->ioctx_private))
380 return (PTR_ERR(ioctx->ioctx_private));
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;