1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/file.c
38 * Lustre Light file operations
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
49 #include <sys/queue.h>
65 #include "llite_lib.h"
67 /* Pack the required supplementary groups into the supplied groups array.
68 * If we don't need to use the groups from the target inode(s) then we
69 * instead pack one or more groups from the user's supplementary group
70 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
71 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
74 LASSERT(suppgids != NULL);
76 if (in_group_p(i1->i_stbuf.st_gid))
77 suppgids[0] = i1->i_stbuf.st_gid;
82 if (in_group_p(i2->i_stbuf.st_gid))
83 suppgids[1] = i2->i_stbuf.st_gid;
91 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
98 LASSERT(i1 != NULL || i2 != NULL);
101 ll_i2gids(data->suppgids, i1, i2);
102 llu_inode2fid(&data->fid1, i1);
104 ll_i2gids(data->suppgids, i2, i1);
105 llu_inode2fid(&data->fid1, i2);
109 llu_inode2fid(&data->fid2, i2);
111 memset(&data->fid2, 0, sizeof(data->fid2));
114 data->namelen = namelen;
115 data->create_mode = mode;
116 data->mod_time = CURRENT_TIME;
120 void obdo_refresh_inode(struct inode *dst,
124 struct intnl_stat *st = llu_i2stat(dst);
125 valid &= src->o_valid;
127 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
128 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
129 src->o_valid, LTIME_S(st->st_mtime),
130 LTIME_S(st->st_ctime),
131 (long)src->o_mtime, (long)src->o_ctime);
133 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
134 LTIME_S(st->st_atime) = src->o_atime;
135 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(st->st_mtime))
136 LTIME_S(st->st_mtime) = src->o_mtime;
137 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
138 LTIME_S(st->st_ctime) = src->o_ctime;
139 if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
140 st->st_size = src->o_size;
141 /* optimum IO size */
142 if (valid & OBD_MD_FLBLKSZ)
143 st->st_blksize = src->o_blksize;
144 /* allocation of space */
145 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
146 st->st_blocks = src->o_blocks;
149 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
151 struct ptlrpc_request *req = it->d.lustre.it_data;
152 struct ll_file_data *fd;
153 struct mds_body *body;
156 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
157 LASSERT(body != NULL); /* reply already checked out */
158 /* and swabbed down */
159 LASSERT(lustre_rep_swabbed(req, DLM_REPLY_REC_OFF));
161 /* already opened? */
162 if (lli->lli_open_count++)
165 LASSERT(!lli->lli_file_data);
167 OBD_ALLOC(fd, sizeof(*fd));
168 /* We can't handle this well without reorganizing ll_file_open and
169 * ll_mdc_close, so don't even try right now. */
172 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
173 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
174 lli->lli_file_data = fd;
176 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
181 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
183 struct inode *inode = pnode->p_base->pb_ino;
184 struct llu_inode_info *lli = llu_i2info(inode);
185 struct intnl_stat *st = llu_i2stat(inode);
186 struct ll_file_data *fd;
187 struct ptlrpc_request *request;
188 struct lookup_intent *it;
189 struct lov_stripe_md *lsm;
193 liblustre_wait_event(0);
195 /* don't do anything for '/' */
196 if (llu_is_root_inode(inode))
199 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
200 LL_GET_INTENT(inode, it);
202 if (!it->d.lustre.it_disposition) {
206 rc = it_open_error(DISP_OPEN_OPEN, it);
208 GOTO(out_release, rc);
210 rc = llu_local_open(lli, it);
214 if (!S_ISREG(st->st_mode))
215 GOTO(out_release, rc = 0);
217 fd = lli->lli_file_data;
221 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
222 CDEBUG(D_INODE, "object creation was delayed\n");
223 GOTO(out_release, rc);
226 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
228 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
231 request = it->d.lustre.it_data;
232 ptlrpc_req_finished(request);
234 it->it_op_release(it);
235 OBD_FREE(it, sizeof(*it));
237 /* libsysio hasn't done anything for O_TRUNC. here we
238 * simply simulate it as open(...); truncate(...); */
239 if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
242 memset(&attr, 0, sizeof(attr));
244 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
245 rc = llu_setattr_raw(inode, &attr);
247 CERROR("error %d truncate in open()\n", rc);
250 liblustre_wait_event(0);
254 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
256 struct mds_body *body;
257 struct lov_mds_md *eadata;
258 struct lov_stripe_md *lsm = NULL;
259 struct obd_trans_info oti = { 0 };
264 /* req is swabbed so this is safe */
265 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
267 if (!(body->valid & OBD_MD_FLEASIZE))
270 if (body->eadatasize == 0) {
271 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
272 GOTO(out, rc = -EPROTO);
275 /* The MDS sent back the EA because we unlinked the last reference
276 * to this file. Use this EA to unlink the objects on the OST.
277 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
278 * check it is complete and sensible. */
279 eadata = lustre_swab_repbuf(request, REPLY_REC_OFF+1, body->eadatasize,
281 LASSERT(eadata != NULL);
282 if (eadata == NULL) {
283 CERROR("Can't unpack MDS EA data\n");
284 GOTO(out, rc = -EPROTO);
287 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
289 CERROR("obd_unpackmd: %d\n", rc);
292 LASSERT(rc >= sizeof(*lsm));
296 GOTO(out_free_memmd, rc = -ENOMEM);
298 oa->o_id = lsm->lsm_object_id;
299 oa->o_mode = body->mode & S_IFMT;
300 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
302 if (body->valid & OBD_MD_FLCOOKIE) {
303 oa->o_valid |= OBD_MD_FLCOOKIE;
305 lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
306 sizeof(struct llog_cookie) *
307 lsm->lsm_stripe_count);
308 if (oti.oti_logcookies == NULL) {
309 oa->o_valid &= ~OBD_MD_FLCOOKIE;
310 body->valid &= ~OBD_MD_FLCOOKIE;
314 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL);
317 CERROR("obd destroy objid 0x"LPX64" error %d\n",
318 lsm->lsm_object_id, rc);
320 obd_free_memmd(llu_i2obdexp(dir), &lsm);
325 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
327 struct llu_inode_info *lli = llu_i2info(inode);
328 struct intnl_stat *st = llu_i2stat(inode);
329 struct ll_file_data *fd = lli->lli_file_data;
330 struct ptlrpc_request *req = NULL;
331 struct obd_client_handle *och = &fd->fd_mds_och;
332 struct obdo obdo = { 0 };
333 struct mdc_op_data data = { { 0 } };
337 /* clear group lock, if present */
338 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
339 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
340 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
341 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
345 obdo.o_id = st->st_ino;
346 obdo.o_valid = OBD_MD_FLID;
347 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
348 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
349 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
350 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
352 obdo_from_inode(&obdo, inode, valid);
354 if (0 /* ll_is_inode_dirty(inode) */) {
355 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
356 obdo.o_valid |= OBD_MD_FLFLAGS;
358 data.fid1 = lli->lli_fid;
359 rc = mdc_close(mdc_exp, &data, &obdo, och, &req);
361 /* We are the last writer, so the MDS has instructed us to get
362 * the file size and any write cookies, then close again. */
363 //ll_queue_done_writing(inode);
366 CERROR("inode %llu close failed: rc %d\n",
367 (long long)st->st_ino, rc);
369 rc = llu_objects_destroy(req, inode);
371 CERROR("inode %llu ll_objects destroy: rc = %d\n",
372 (long long)st->st_ino, rc);
375 mdc_clear_open_replay_data(och);
376 ptlrpc_req_finished(req);
377 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
378 lli->lli_file_data = NULL;
379 OBD_FREE(fd, sizeof(*fd));
384 int llu_file_release(struct inode *inode)
386 struct ll_file_data *fd;
387 struct llu_sb_info *sbi = llu_i2sbi(inode);
388 struct llu_inode_info *lli = llu_i2info(inode);
392 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
393 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
395 if (llu_is_root_inode(inode))
398 /* still opened by others? */
399 if (--lli->lli_open_count)
402 fd = lli->lli_file_data;
403 if (!fd) /* no process opened the file after an mcreate */
406 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
414 * libsysio require us return 0
416 int llu_iop_close(struct inode *inode)
419 struct ldlm_res_id res_id =
420 { .name = {llu_i2stat(inode)->st_ino,
421 (__u64)llu_i2info(inode)->lli_st_generation, LDLM_FLOCK} };
422 struct lustre_handle lockh = {0};
424 liblustre_wait_event(0);
426 /* If we have posix locks on this file - clear all of those */
428 class_exp2obd(llu_i2mdcexp(inode))->obd_namespace,
429 LDLM_FL_BLOCK_GRANTED|LDLM_FL_TEST_LOCK|LDLM_FL_CBPENDING,
430 &res_id, LDLM_FLOCK, NULL, LCK_PR|LCK_PW, &lockh)) {
431 struct file_lock lock;
432 lock.fl_type = F_UNLCK;
433 lock.fl_flags = FL_POSIX;
435 lock.fl_end = OFFSET_MAX;
436 lock.fl_pid = getpid();
437 lock.fl_notify = NULL;
438 lock.fl_insert = NULL;
439 lock.fl_remove = NULL;
440 lock.fl_owner = NULL;
443 llu_file_flock(inode, F_SETLK, &lock);
446 rc = llu_file_release(inode);
448 CERROR("file close error %d\n", rc);
450 /* if open count == 0 && stale_flag is set, should we
451 * remove the inode immediately? */
452 liblustre_wait_idle();
456 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
460 liblustre_wait_event(0);
462 if (off < 0 || off > ll_file_maxbytes(ino))
468 /* this isn't where truncate starts. roughly:
469 * llu_iop_{open,setattr}->llu_setattr_raw->llu_vmtruncate->llu_truncate
470 * we grab the lock back in setattr_raw to avoid races. */
471 static void llu_truncate(struct inode *inode, obd_flag flags)
473 struct llu_inode_info *lli = llu_i2info(inode);
474 struct intnl_stat *st = llu_i2stat(inode);
475 struct obd_info oinfo = { { { 0 } } };
476 struct obdo oa = { 0 };
480 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p) to %llu\n",
481 (long long)st->st_ino, lli->lli_st_generation, inode,
482 (long long)st->st_size);
485 CDEBUG(D_INODE, "truncate on inode %llu with no objects\n",
486 (long long)st->st_ino);
491 oinfo.oi_md = lli->lli_smd;
492 oinfo.oi_policy.l_extent.start = st->st_size;
493 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
495 oa.o_id = lli->lli_smd->lsm_object_id;
496 oa.o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
497 oa.o_flags = flags; /* We don't actually want to copy inode flags */
499 valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLATIME;
500 if (flags & OBD_FL_TRUNCLOCK) {
503 * 1. do not use inode's timestamps because concurrent
504 * stat might fill the inode with out-of-date times,
505 * send current instead
507 * 2.do no update lsm, as long as stat (via
508 * llu_glimpse_size) will bring attributes from osts
510 oa.o_mtime = oa.o_ctime = CURRENT_TIME;
511 oa.o_valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
513 /* truncate under locks
515 * 1. update inode's mtime and ctime as long as
516 * concurrent stat (via llu_glimpse_size) might bring
519 * 2. update lsm so that next stat (via
520 * llu_glimpse_size) could get correct values in lsm */
521 struct ost_lvb xtimes;
523 lov_stripe_lock(lli->lli_smd);
524 st->st_mtime = st->st_ctime = CURRENT_TIME;
525 xtimes.lvb_mtime = st->st_mtime;
526 xtimes.lvb_ctime = st->st_ctime;
527 obd_update_lvb(llu_i2obdexp(inode), lli->lli_smd, &xtimes,
528 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
529 lov_stripe_unlock(lli->lli_smd);
531 valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
533 obdo_from_inode(&oa, inode, valid);
535 obd_adjust_kms(llu_i2obdexp(inode), lli->lli_smd, st->st_size, 1);
537 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
538 oa.o_id, (long long)st->st_size);
540 /* truncate == punch from new size to absolute end of file */
541 rc = obd_punch_rqset(llu_i2obdexp(inode), &oinfo, NULL);
543 CERROR("obd_truncate fails (%d) ino %llu\n",
544 rc, (long long)st->st_ino);
546 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
547 OBD_MD_FLATIME | OBD_MD_FLMTIME |
554 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag flags)
556 llu_i2stat(inode)->st_size = offset;
559 * llu_truncate() is only called from this
560 * point. llu_vmtruncate/llu_truncate split exists to mimic the
561 * structure of Linux VFS truncate code path.
564 llu_truncate(inode, flags);