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_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
75 struct inode *i2, const char *name, int namelen,
78 LASSERT(i1 != NULL || i2 != NULL);
80 memset(op_data, 0, sizeof(*op_data));
83 ll_i2gids(op_data->op_suppgids, i1, i2);
84 op_data->op_fid1 = *ll_inode2fid(i1);
86 ll_i2gids(op_data->op_suppgids, i2, i1);
87 op_data->op_fid1 = *ll_inode2fid(i2);
91 op_data->op_fid2 = *ll_inode2fid(i2);
93 fid_zero(&op_data->op_fid2);
95 op_data->op_opc = opc;
96 op_data->op_name = name;
97 op_data->op_mode = mode;
98 op_data->op_namelen = namelen;
99 op_data->op_mod_time = CURRENT_TIME;
102 void llu_finish_md_op_data(struct md_op_data *op_data)
104 OBD_FREE_PTR(op_data);
107 void obdo_refresh_inode(struct inode *dst,
111 struct intnl_stat *st = llu_i2stat(dst);
112 valid &= src->o_valid;
114 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
115 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
116 src->o_valid, LTIME_S(st->st_mtime),
117 LTIME_S(st->st_ctime),
118 (long)src->o_mtime, (long)src->o_ctime);
120 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
121 LTIME_S(st->st_atime) = src->o_atime;
123 /* mtime is always updated with ctime, but can be set in past.
124 As write and utime(2) may happen within 1 second, and utime's
125 mtime has a priority over write's one, leave mtime from mds
126 for the same ctimes. */
127 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime)) {
128 LTIME_S(st->st_ctime) = src->o_ctime;
129 if (valid & OBD_MD_FLMTIME)
130 LTIME_S(st->st_mtime) = src->o_mtime;
132 if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
133 st->st_size = src->o_size;
134 /* optimum IO size */
135 if (valid & OBD_MD_FLBLKSZ)
136 st->st_blksize = src->o_blksize;
137 /* allocation of space */
138 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
139 st->st_blocks = src->o_blocks;
142 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
144 struct ptlrpc_request *req = it->d.lustre.it_data;
145 struct ll_file_data *fd;
146 struct mdt_body *body;
149 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
150 LASSERT(body != NULL); /* reply already checked out */
151 LASSERT_REPSWABBED(req, DLM_REPLY_REC_OFF); /* and swabbed down */
153 /* already opened? */
154 if (lli->lli_open_count++)
157 LASSERT(!lli->lli_file_data);
159 OBD_ALLOC(fd, sizeof(*fd));
160 /* We can't handle this well without reorganizing ll_file_open and
161 * ll_md_close, so don't even try right now. */
164 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
165 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
166 lli->lli_file_data = fd;
168 md_set_open_replay_data(lli->lli_sbi->ll_md_exp,
169 &fd->fd_mds_och, it->d.lustre.it_data);
174 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
176 struct inode *inode = pnode->p_base->pb_ino;
177 struct llu_inode_info *lli = llu_i2info(inode);
178 struct intnl_stat *st = llu_i2stat(inode);
179 struct ll_file_data *fd;
180 struct ptlrpc_request *request;
181 struct lookup_intent *it;
182 struct lov_stripe_md *lsm;
186 liblustre_wait_event(0);
188 /* don't do anything for '/' */
189 if (llu_is_root_inode(inode))
192 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
193 LL_GET_INTENT(inode, it);
195 if (!it->d.lustre.it_disposition) {
199 rc = it_open_error(DISP_OPEN_OPEN, it);
201 GOTO(out_release, rc);
203 rc = llu_local_open(lli, it);
207 if (!S_ISREG(st->st_mode))
208 GOTO(out_release, rc = 0);
210 fd = lli->lli_file_data;
214 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
215 CDEBUG(D_INODE, "object creation was delayed\n");
216 GOTO(out_release, rc);
219 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
221 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
224 request = it->d.lustre.it_data;
225 ptlrpc_req_finished(request);
227 it->it_op_release(it);
228 OBD_FREE(it, sizeof(*it));
230 /* libsysio hasn't done anything for O_TRUNC. here we
231 * simply simulate it as open(...); truncate(...); */
232 if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
235 memset(&attr, 0, sizeof(attr));
237 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
238 rc = llu_setattr_raw(inode, &attr);
240 CERROR("error %d truncate in open()\n", rc);
243 liblustre_wait_event(0);
247 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
249 struct mdt_body *body;
250 struct lov_mds_md *eadata;
251 struct lov_stripe_md *lsm = NULL;
252 struct obd_trans_info oti = { 0 };
257 /* req is swabbed so this is safe */
258 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
260 if (!(body->valid & OBD_MD_FLEASIZE))
263 if (body->eadatasize == 0) {
264 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
265 GOTO(out, rc = -EPROTO);
268 /* The MDS sent back the EA because we unlinked the last reference
269 * to this file. Use this EA to unlink the objects on the OST.
270 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
271 * check it is complete and sensible. */
272 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF+1, body->eadatasize,
274 LASSERT(eadata != NULL);
275 if (eadata == NULL) {
276 CERROR("Can't unpack MDS EA data\n");
277 GOTO(out, rc = -EPROTO);
280 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
282 CERROR("obd_unpackmd: %d\n", rc);
285 LASSERT(rc >= sizeof(*lsm));
289 GOTO(out_free_memmd, rc = -ENOMEM);
291 oa->o_id = lsm->lsm_object_id;
292 oa->o_gr = lsm->lsm_object_gr;
293 oa->o_mode = body->mode & S_IFMT;
294 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
296 if (body->valid & OBD_MD_FLCOOKIE) {
297 oa->o_valid |= OBD_MD_FLCOOKIE;
299 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
300 sizeof(struct llog_cookie) *
301 lsm->lsm_stripe_count);
302 if (oti.oti_logcookies == NULL) {
303 oa->o_valid &= ~OBD_MD_FLCOOKIE;
304 body->valid &= ~OBD_MD_FLCOOKIE;
308 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL);
311 CERROR("obd destroy objid 0x"LPX64" error %d\n",
312 lsm->lsm_object_id, rc);
314 obd_free_memmd(llu_i2obdexp(dir), &lsm);
319 int llu_sizeonmds_update(struct inode *inode, struct lustre_handle *fh)
321 struct llu_inode_info *lli = llu_i2info(inode);
322 struct md_op_data op_data;
327 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
329 rc = llu_inode_getattr(inode, &oa);
331 CERROR("inode_getattr failed (%d): unable to send a "
332 "Size-on-MDS attribute update for inode %llu/%lu\n",
333 rc, (long long)llu_i2stat(inode)->st_ino,
334 lli->lli_st_generation);
338 md_from_obdo(&op_data, &oa, oa.o_valid);
339 memcpy(&op_data.op_handle, fh, sizeof(*fh));
340 op_data.op_flags |= MF_SOM_CHANGE;
342 rc = llu_md_setattr(inode, &op_data);
346 int llu_md_close(struct obd_export *md_exp, struct inode *inode)
348 struct llu_inode_info *lli = llu_i2info(inode);
349 struct ll_file_data *fd = lli->lli_file_data;
350 struct ptlrpc_request *req = NULL;
351 struct obd_client_handle *och = &fd->fd_mds_och;
352 struct intnl_stat *st = llu_i2stat(inode);
353 struct md_op_data op_data = { { 0 } };
357 /* clear group lock, if present */
358 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
359 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
360 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
361 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
365 op_data.op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
366 ATTR_MTIME_SET | ATTR_CTIME_SET;
368 if (fd->fd_flags & FMODE_WRITE) {
369 if (!S_ISREG(llu_i2stat(inode)->st_mode)) {
370 op_data.op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
372 /* Inode cannot be dirty. Close the epoch. */
373 op_data.op_flags |= MF_EPOCH_CLOSE;
374 /* XXX: Send CHANGE flag only if Size-on-MDS inode attributes
375 * are really changed. */
376 op_data.op_flags |= MF_SOM_CHANGE;
378 /* Pack Size-on-MDS attributes if we are in IO epoch and
379 * attributes are valid. */
380 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
381 if (!llu_local_size(inode))
382 op_data.op_attr.ia_valid |=
383 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
386 op_data.op_fid1 = lli->lli_fid;
387 op_data.op_attr.ia_atime = st->st_atime;
388 op_data.op_attr.ia_mtime = st->st_mtime;
389 op_data.op_attr.ia_ctime = st->st_ctime;
390 op_data.op_attr.ia_size = st->st_size;
391 op_data.op_attr_blocks = st->st_blocks;
392 op_data.op_attr.ia_attr_flags = lli->lli_st_flags;
393 op_data.op_ioepoch = lli->lli_ioepoch;
394 memcpy(&op_data.op_handle, &och->och_fh, sizeof(op_data.op_handle));
396 rc = md_close(md_exp, &op_data, och, &req);
398 /* We are the last writer, so the MDS has instructed us to get
399 * the file size and any write cookies, then close again. */
400 LASSERT(fd->fd_flags & FMODE_WRITE);
401 rc = llu_sizeonmds_update(inode, &och->och_fh);
403 CERROR("inode %llu mdc Size-on-MDS update failed: "
404 "rc = %d\n", (long long)st->st_ino, rc);
408 CERROR("inode %llu close failed: rc %d\n",
409 (long long)st->st_ino, rc);
411 rc = llu_objects_destroy(req, inode);
413 CERROR("inode %llu ll_objects destroy: rc = %d\n",
414 (long long)st->st_ino, rc);
417 md_clear_open_replay_data(md_exp, och);
418 ptlrpc_req_finished(req);
419 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
420 lli->lli_file_data = NULL;
421 OBD_FREE(fd, sizeof(*fd));
426 int llu_file_release(struct inode *inode)
428 struct ll_file_data *fd;
429 struct llu_sb_info *sbi = llu_i2sbi(inode);
430 struct llu_inode_info *lli = llu_i2info(inode);
434 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
435 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
437 if (llu_is_root_inode(inode))
440 /* still opened by others? */
441 if (--lli->lli_open_count)
444 fd = lli->lli_file_data;
445 if (!fd) /* no process opened the file after an mcreate */
448 rc2 = llu_md_close(sbi->ll_md_exp, inode);
456 * libsysio require us return 0
458 int llu_iop_close(struct inode *inode)
462 liblustre_wait_event(0);
464 rc = llu_file_release(inode);
466 CERROR("file close error %d\n", rc);
468 /* if open count == 0 && stale_flag is set, should we
469 * remove the inode immediately? */
470 liblustre_wait_event(0);
474 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
478 liblustre_wait_event(0);
480 if (off < 0 || off > ll_file_maxbytes(ino))
486 /* this isn't where truncate starts. roughly:
487 * llu_iop_{open,setattr}->llu_setattr_raw->llu_vmtruncate->llu_truncate
488 * we grab the lock back in setattr_raw to avoid races. */
489 static void llu_truncate(struct inode *inode, obd_flag flags)
491 struct llu_inode_info *lli = llu_i2info(inode);
492 struct intnl_stat *st = llu_i2stat(inode);
493 struct obd_info oinfo = { { { 0 } } };
494 struct obdo oa = { 0 };
497 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p) to %llu\n",
498 (long long)st->st_ino, lli->lli_st_generation, inode,
499 (long long)st->st_size);
502 CDEBUG(D_INODE, "truncate on inode %llu with no objects\n",
503 (long long)st->st_ino);
508 oinfo.oi_md = lli->lli_smd;
509 oinfo.oi_policy.l_extent.start = st->st_size;
510 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
512 oa.o_id = lli->lli_smd->lsm_object_id;
513 oa.o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
514 oa.o_flags = flags; /* We don't actually want to copy inode flags */
516 obdo_from_inode(&oa, inode,
517 OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLATIME |
518 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
520 obd_adjust_kms(llu_i2obdexp(inode), lli->lli_smd, st->st_size, 1);
522 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
523 oa.o_id, (long long)st->st_size);
525 /* truncate == punch from new size to absolute end of file */
526 rc = obd_punch_rqset(llu_i2obdexp(inode), &oinfo, NULL);
528 CERROR("obd_truncate fails (%d) ino %llu\n",
529 rc, (long long)st->st_ino);
531 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
532 OBD_MD_FLATIME | OBD_MD_FLMTIME |
539 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag flags)
541 llu_i2stat(inode)->st_size = offset;
544 * llu_truncate() is only called from this
545 * point. llu_vmtruncate/llu_truncate split exists to mimic the
546 * structure of Linux VFS truncate code path.
549 llu_truncate(inode, flags);