1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super 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(&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;
170 request = it->d.lustre.it_data;
171 ptlrpc_req_finished(request);
173 it->it_op_release(it);
174 OBD_FREE(it, sizeof(*it));
179 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
181 struct mds_body *body;
182 struct lov_mds_md *eadata;
183 struct lov_stripe_md *lsm = NULL;
184 struct obd_trans_info oti = { 0 };
189 /* req is swabbed so this is safe */
190 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
192 if (!(body->valid & OBD_MD_FLEASIZE))
195 if (body->eadatasize == 0) {
196 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
197 GOTO(out, rc = -EPROTO);
200 /* The MDS sent back the EA because we unlinked the last reference
201 * to this file. Use this EA to unlink the objects on the OST.
202 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
203 * check it is complete and sensible. */
204 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
205 LASSERT(eadata != NULL);
206 if (eadata == NULL) {
207 CERROR("Can't unpack MDS EA data\n");
208 GOTO(out, rc = -EPROTO);
211 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
213 CERROR("obd_unpackmd: %d\n", rc);
216 LASSERT(rc >= sizeof(*lsm));
220 GOTO(out_free_memmd, rc = -ENOMEM);
222 oa->o_id = lsm->lsm_object_id;
223 oa->o_mode = body->mode & S_IFMT;
224 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
226 if (body->valid & OBD_MD_FLCOOKIE) {
227 oa->o_valid |= OBD_MD_FLCOOKIE;
229 lustre_msg_buf(request->rq_repmsg, 2,
230 sizeof(struct llog_cookie) *
231 lsm->lsm_stripe_count);
232 if (oti.oti_logcookies == NULL) {
233 oa->o_valid &= ~OBD_MD_FLCOOKIE;
234 body->valid &= ~OBD_MD_FLCOOKIE;
238 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
241 CERROR("obd destroy objid 0x"LPX64" error %d\n",
242 lsm->lsm_object_id, rc);
244 obd_free_memmd(llu_i2obdexp(dir), &lsm);
249 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
251 struct llu_inode_info *lli = llu_i2info(inode);
252 struct ll_file_data *fd = lli->lli_file_data;
253 struct ptlrpc_request *req = NULL;
254 struct obd_client_handle *och = &fd->fd_mds_och;
260 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
261 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
263 memset(&obdo, 0, sizeof(obdo));
264 obdo.o_id = lli->lli_st_ino;
265 obdo.o_mode = lli->lli_st_mode;
266 obdo.o_size = lli->lli_st_size;
267 obdo.o_blocks = lli->lli_st_blocks;
268 if (0 /* ll_is_inode_dirty(inode) */) {
269 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
270 valid |= OBD_MD_FLFLAGS;
272 obdo.o_valid = valid;
273 rc = mdc_close(mdc_exp, &obdo, och, &req);
275 /* We are the last writer, so the MDS has instructed us to get
276 * the file size and any write cookies, then close again. */
277 //ll_queue_done_writing(inode);
280 CERROR("inode %lu close failed: rc = %d\n", lli->lli_st_ino, rc);
282 rc = llu_objects_destroy(req, inode);
284 CERROR("inode %lu ll_objects destroy: rc = %d\n",
285 lli->lli_st_ino, rc);
288 mdc_clear_open_replay_data(och);
289 ptlrpc_req_finished(req);
290 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
291 lli->lli_file_data = NULL;
292 OBD_FREE(fd, sizeof(*fd));
297 int llu_file_release(struct inode *inode)
299 struct ll_file_data *fd;
300 struct llu_sb_info *sbi = llu_i2sbi(inode);
301 struct llu_inode_info *lli = llu_i2info(inode);
305 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
306 lli->lli_st_generation);
308 /* XXX don't do anything for '/'. but how to find the root pnode? */
310 /* still opened by others? */
311 if (--lli->lli_open_count)
314 fd = lli->lli_file_data;
315 if (!fd) /* no process opened the file after an mcreate */
318 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
325 int llu_iop_close(struct inode *inode)
329 rc = llu_file_release(inode);
330 if (!llu_i2info(inode)->lli_open_count)
331 llu_i2info(inode)->lli_stale_flag = 1;
335 int llu_iop_ipreadv(struct inode *ino,
340 if (!ioctx->ioctx_iovlen)
342 if (ioctx->ioctx_iovlen < 0)
345 ioctx->ioctx_private = llu_file_read(ino,
348 ioctx->ioctx_offset);
349 if (IS_ERR(ioctx->ioctx_private))
350 return (PTR_ERR(ioctx->ioctx_private));
355 int llu_iop_ipwritev(struct inode *ino,
360 if (!ioctx->ioctx_iovlen)
362 if (ioctx->ioctx_iovlen < 0)
365 ioctx->ioctx_private = llu_file_write(ino,
368 ioctx->ioctx_offset);
369 if (IS_ERR(ioctx->ioctx_private))
370 return (PTR_ERR(ioctx->ioctx_private));
375 /* this isn't where truncate starts. roughly:
376 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
377 * we grab the lock back in setattr_raw to avoid races. */
378 static void llu_truncate(struct inode *inode)
380 struct llu_inode_info *lli = llu_i2info(inode);
381 struct lov_stripe_md *lsm = lli->lli_smd;
382 struct obdo oa = {0};
385 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
386 lli->lli_st_generation);
389 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
394 oa.o_id = lsm->lsm_object_id;
395 oa.o_valid = OBD_MD_FLID;
396 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
397 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
399 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
400 oa.o_id, lli->lli_st_size);
402 /* truncate == punch from new size to absolute end of file */
403 err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
404 OBD_OBJECT_EOF, NULL);
406 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
408 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
409 OBD_MD_FLATIME | OBD_MD_FLMTIME |
416 int llu_vmtruncate(struct inode * inode, loff_t offset)
418 struct llu_inode_info *lli = llu_i2info(inode);
420 lli->lli_st_size = offset;