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>
32 #include <sys/queue.h>
48 #include "llite_lib.h"
50 /* Pack the required supplementary groups into the supplied groups array.
51 * If we don't need to use the groups from the target inode(s) then we
52 * instead pack one or more groups from the user's supplementary group
53 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
54 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
57 LASSERT(suppgids != NULL);
59 if (in_group_p(i1->i_stbuf.st_gid))
60 suppgids[0] = i1->i_stbuf.st_gid;
65 if (in_group_p(i2->i_stbuf.st_gid))
66 suppgids[1] = i2->i_stbuf.st_gid;
74 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
81 LASSERT(i1 != NULL || i2 != NULL);
84 ll_i2gids(data->suppgids, i1, i2);
85 ll_inode2fid(&data->fid1, i1);
87 ll_i2gids(data->suppgids, i2, i1);
88 ll_inode2fid(&data->fid1, i2);
92 ll_inode2fid(&data->fid2, i2);
94 memset(&data->fid2, 0, sizeof(data->fid2));
97 data->namelen = namelen;
98 data->create_mode = mode;
99 data->mod_time = CURRENT_TIME;
102 void obdo_refresh_inode(struct inode *dst,
106 struct intnl_stat *st = llu_i2stat(dst);
107 valid &= src->o_valid;
109 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
110 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
111 src->o_valid, LTIME_S(st->st_mtime),
112 LTIME_S(st->st_ctime),
113 (long)src->o_mtime, (long)src->o_ctime);
115 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
116 LTIME_S(st->st_atime) = src->o_atime;
118 /* mtime is always updated with ctime, but can be set in past.
119 As write and utime(2) may happen within 1 second, and utime's
120 mtime has a priority over write's one, leave mtime from mds
121 for the same ctimes. */
122 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime)) {
123 LTIME_S(st->st_ctime) = src->o_ctime;
124 if (valid & OBD_MD_FLMTIME)
125 LTIME_S(st->st_mtime) = src->o_mtime;
127 if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
128 st->st_size = src->o_size;
129 /* optimum IO size */
130 if (valid & OBD_MD_FLBLKSZ)
131 st->st_blksize = src->o_blksize;
132 /* allocation of space */
133 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
134 st->st_blocks = src->o_blocks;
137 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
139 struct ptlrpc_request *req = it->d.lustre.it_data;
140 struct ll_file_data *fd;
141 struct mds_body *body;
144 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
145 LASSERT(body != NULL); /* reply already checked out */
146 LASSERT_REPSWABBED(req, DLM_REPLY_REC_OFF); /* and swabbed down */
148 /* already opened? */
149 if (lli->lli_open_count++)
152 LASSERT(!lli->lli_file_data);
154 OBD_ALLOC(fd, sizeof(*fd));
155 /* We can't handle this well without reorganizing ll_file_open and
156 * ll_mdc_close, so don't even try right now. */
159 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
160 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
161 lli->lli_file_data = fd;
163 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
168 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
170 struct inode *inode = pnode->p_base->pb_ino;
171 struct llu_inode_info *lli = llu_i2info(inode);
172 struct intnl_stat *st = llu_i2stat(inode);
173 struct ll_file_data *fd;
174 struct ptlrpc_request *request;
175 struct lookup_intent *it;
176 struct lov_stripe_md *lsm;
180 liblustre_wait_event(0);
182 /* don't do anything for '/' */
183 if (llu_is_root_inode(inode))
186 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
187 LL_GET_INTENT(inode, it);
189 if (!it->d.lustre.it_disposition) {
193 rc = it_open_error(DISP_OPEN_OPEN, it);
195 GOTO(out_release, rc);
197 rc = llu_local_open(lli, it);
201 if (!S_ISREG(st->st_mode))
202 GOTO(out_release, rc = 0);
204 fd = lli->lli_file_data;
208 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
209 CDEBUG(D_INODE, "object creation was delayed\n");
210 GOTO(out_release, rc);
213 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
215 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
218 request = it->d.lustre.it_data;
219 ptlrpc_req_finished(request);
221 it->it_op_release(it);
222 OBD_FREE(it, sizeof(*it));
224 /* libsysio hasn't done anything for O_TRUNC. here we
225 * simply simulate it as open(...); truncate(...); */
226 if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
229 memset(&attr, 0, sizeof(attr));
231 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
232 rc = llu_setattr_raw(inode, &attr);
234 CERROR("error %d truncate in open()\n", rc);
237 liblustre_wait_event(0);
241 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
243 struct mds_body *body;
244 struct lov_mds_md *eadata;
245 struct lov_stripe_md *lsm = NULL;
246 struct obd_trans_info oti = { 0 };
251 /* req is swabbed so this is safe */
252 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
254 if (!(body->valid & OBD_MD_FLEASIZE))
257 if (body->eadatasize == 0) {
258 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
259 GOTO(out, rc = -EPROTO);
262 /* The MDS sent back the EA because we unlinked the last reference
263 * to this file. Use this EA to unlink the objects on the OST.
264 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
265 * check it is complete and sensible. */
266 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF+1, body->eadatasize,
268 LASSERT(eadata != NULL);
269 if (eadata == NULL) {
270 CERROR("Can't unpack MDS EA data\n");
271 GOTO(out, rc = -EPROTO);
274 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
276 CERROR("obd_unpackmd: %d\n", rc);
279 LASSERT(rc >= sizeof(*lsm));
283 GOTO(out_free_memmd, rc = -ENOMEM);
285 oa->o_id = lsm->lsm_object_id;
286 oa->o_mode = body->mode & S_IFMT;
287 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
289 if (body->valid & OBD_MD_FLCOOKIE) {
290 oa->o_valid |= OBD_MD_FLCOOKIE;
292 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
293 sizeof(struct llog_cookie) *
294 lsm->lsm_stripe_count);
295 if (oti.oti_logcookies == NULL) {
296 oa->o_valid &= ~OBD_MD_FLCOOKIE;
297 body->valid &= ~OBD_MD_FLCOOKIE;
301 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL);
304 CERROR("obd destroy objid 0x"LPX64" error %d\n",
305 lsm->lsm_object_id, rc);
307 obd_free_memmd(llu_i2obdexp(dir), &lsm);
312 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
314 struct llu_inode_info *lli = llu_i2info(inode);
315 struct intnl_stat *st = llu_i2stat(inode);
316 struct ll_file_data *fd = lli->lli_file_data;
317 struct ptlrpc_request *req = NULL;
318 struct obd_client_handle *och = &fd->fd_mds_och;
323 /* clear group lock, if present */
324 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
325 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
326 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
327 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
331 obdo.o_id = st->st_ino;
332 obdo.o_valid = OBD_MD_FLID;
333 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
334 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
335 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
336 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
338 obdo_from_inode(&obdo, inode, valid);
340 if (0 /* ll_is_inode_dirty(inode) */) {
341 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
342 obdo.o_valid |= OBD_MD_FLFLAGS;
344 rc = mdc_close(mdc_exp, &obdo, och, &req);
346 /* We are the last writer, so the MDS has instructed us to get
347 * the file size and any write cookies, then close again. */
348 //ll_queue_done_writing(inode);
351 CERROR("inode %llu close failed: rc %d\n",
352 (long long)st->st_ino, rc);
354 rc = llu_objects_destroy(req, inode);
356 CERROR("inode %llu ll_objects destroy: rc = %d\n",
357 (long long)st->st_ino, rc);
360 mdc_clear_open_replay_data(och);
361 ptlrpc_req_finished(req);
362 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
363 lli->lli_file_data = NULL;
364 OBD_FREE(fd, sizeof(*fd));
369 int llu_file_release(struct inode *inode)
371 struct ll_file_data *fd;
372 struct llu_sb_info *sbi = llu_i2sbi(inode);
373 struct llu_inode_info *lli = llu_i2info(inode);
377 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
378 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
380 if (llu_is_root_inode(inode))
383 /* still opened by others? */
384 if (--lli->lli_open_count)
387 fd = lli->lli_file_data;
388 if (!fd) /* no process opened the file after an mcreate */
391 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
399 * libsysio require us return 0
401 int llu_iop_close(struct inode *inode)
405 liblustre_wait_event(0);
407 rc = llu_file_release(inode);
409 CERROR("file close error %d\n", rc);
411 /* if open count == 0 && stale_flag is set, should we
412 * remove the inode immediately? */
413 liblustre_wait_event(0);
417 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
421 liblustre_wait_event(0);
423 if (off < 0 || off > ll_file_maxbytes(ino))
429 /* this isn't where truncate starts. roughly:
430 * llu_iop_{open,setattr}->llu_setattr_raw->llu_vmtruncate->llu_truncate
431 * we grab the lock back in setattr_raw to avoid races. */
432 static void llu_truncate(struct inode *inode, obd_flag flags)
434 struct llu_inode_info *lli = llu_i2info(inode);
435 struct intnl_stat *st = llu_i2stat(inode);
436 struct obd_info oinfo = { { { 0 } } };
437 struct obdo oa = { 0 };
440 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p) to %llu\n",
441 (long long)st->st_ino, lli->lli_st_generation, inode,
442 (long long)st->st_size);
445 CDEBUG(D_INODE, "truncate on inode %llu with no objects\n",
446 (long long)st->st_ino);
451 oinfo.oi_md = lli->lli_smd;
452 oinfo.oi_policy.l_extent.start = st->st_size;
453 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
455 oa.o_id = lli->lli_smd->lsm_object_id;
456 oa.o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
457 oa.o_flags = flags; /* We don't actually want to copy inode flags */
459 obdo_from_inode(&oa, inode,
460 OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLATIME |
461 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
463 obd_adjust_kms(llu_i2obdexp(inode), lli->lli_smd, st->st_size, 1);
465 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
466 oa.o_id, (long long)st->st_size);
468 /* truncate == punch from new size to absolute end of file */
469 rc = obd_punch_rqset(llu_i2obdexp(inode), &oinfo, NULL);
471 CERROR("obd_truncate fails (%d) ino %llu\n",
472 rc, (long long)st->st_ino);
474 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
475 OBD_MD_FLATIME | OBD_MD_FLMTIME |
482 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag flags)
484 llu_i2stat(inode)->st_size = offset;
487 * llu_truncate() is only called from this
488 * point. llu_vmtruncate/llu_truncate split exists to mimic the
489 * structure of Linux VFS truncate code path.
492 llu_truncate(inode, flags);