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_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
92 struct inode *i2, const char *name, int namelen,
95 LASSERT(i1 != NULL || i2 != NULL);
99 ll_i2gids(op_data->op_suppgids, i1, i2);
100 op_data->op_fid1 = *ll_inode2fid(i1);
102 ll_i2gids(op_data->op_suppgids, i2, i1);
103 op_data->op_fid1 = *ll_inode2fid(i2);
107 op_data->op_fid2 = *ll_inode2fid(i2);
109 fid_zero(&op_data->op_fid2);
111 op_data->op_opc = opc;
112 op_data->op_name = name;
113 op_data->op_mode = mode;
114 op_data->op_namelen = namelen;
115 op_data->op_mod_time = CURRENT_TIME;
116 op_data->op_data = NULL;
119 void llu_finish_md_op_data(struct md_op_data *op_data)
121 OBD_FREE_PTR(op_data);
124 void obdo_refresh_inode(struct inode *dst,
128 struct intnl_stat *st = llu_i2stat(dst);
129 valid &= src->o_valid;
131 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
132 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
134 src->o_valid, LTIME_S(st->st_mtime),
135 LTIME_S(st->st_ctime),
136 (long)src->o_mtime, (long)src->o_ctime);
138 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
139 LTIME_S(st->st_atime) = src->o_atime;
141 /* mtime is always updated with ctime, but can be set in past.
142 As write and utime(2) may happen within 1 second, and utime's
143 mtime has a priority over write's one, leave mtime from mds
144 for the same ctimes. */
145 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime)) {
146 LTIME_S(st->st_ctime) = src->o_ctime;
147 if (valid & OBD_MD_FLMTIME)
148 LTIME_S(st->st_mtime) = src->o_mtime;
150 if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
151 st->st_size = src->o_size;
152 /* optimum IO size */
153 if (valid & OBD_MD_FLBLKSZ)
154 st->st_blksize = src->o_blksize;
155 /* allocation of space */
156 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
157 st->st_blocks = src->o_blocks;
160 void llu_ioepoch_open(struct llu_inode_info *lli, __u64 ioepoch)
162 if (ioepoch && lli->lli_ioepoch != ioepoch) {
163 lli->lli_ioepoch = ioepoch;
164 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for truncate\n",
165 ioepoch, PFID(&lli->lli_fid));
169 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
171 struct ptlrpc_request *req = it->d.lustre.it_data;
172 struct ll_file_data *fd;
173 struct mdt_body *body;
176 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
177 LASSERT(body != NULL);
179 /* already opened? */
180 if (lli->lli_open_count++)
183 LASSERT(!lli->lli_file_data);
185 OBD_ALLOC(fd, sizeof(*fd));
186 /* We can't handle this well without reorganizing ll_file_open and
187 * ll_md_close, so don't even try right now. */
190 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
191 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
192 fd->fd_mds_och.och_fid = lli->lli_fid;
193 lli->lli_file_data = fd;
194 llu_ioepoch_open(lli, body->ioepoch);
195 md_set_open_replay_data(lli->lli_sbi->ll_md_exp,
196 &fd->fd_mds_och, it->d.lustre.it_data);
201 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
203 struct inode *inode = pnode->p_base->pb_ino;
204 struct llu_inode_info *lli = llu_i2info(inode);
205 struct intnl_stat *st = llu_i2stat(inode);
206 struct ll_file_data *fd;
207 struct ptlrpc_request *request;
208 struct lookup_intent *it;
209 struct lov_stripe_md *lsm;
213 liblustre_wait_event(0);
215 /* don't do anything for '/' */
216 if (llu_is_root_inode(inode))
219 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
220 LL_GET_INTENT(inode, it);
222 if (!it->d.lustre.it_disposition) {
226 rc = it_open_error(DISP_OPEN_OPEN, it);
228 GOTO(out_release, rc);
230 rc = llu_local_open(lli, it);
234 if (!S_ISREG(st->st_mode))
235 GOTO(out_release, rc = 0);
237 fd = lli->lli_file_data;
241 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
242 CDEBUG(D_INODE, "object creation was delayed\n");
243 GOTO(out_release, rc);
246 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
248 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
251 request = it->d.lustre.it_data;
252 ptlrpc_req_finished(request);
254 it->it_op_release(it);
255 OBD_FREE(it, sizeof(*it));
257 /* libsysio hasn't done anything for O_TRUNC. here we
258 * simply simulate it as open(...); truncate(...); */
259 if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
262 memset(&attr, 0, sizeof(attr));
264 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
265 rc = llu_setattr_raw(inode, &attr);
267 CERROR("error %d truncate in open()\n", rc);
270 liblustre_wait_event(0);
274 int llu_objects_destroy(struct ptlrpc_request *req, struct inode *dir)
276 struct mdt_body *body;
277 struct lov_mds_md *eadata;
278 struct lov_stripe_md *lsm = NULL;
279 struct obd_trans_info oti = { 0 };
284 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
286 if (!(body->valid & OBD_MD_FLEASIZE))
289 if (body->eadatasize == 0) {
290 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
291 GOTO(out, rc = -EPROTO);
294 /* The MDS sent back the EA because we unlinked the last reference
295 * to this file. Use this EA to unlink the objects on the OST.
296 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
297 * check it is complete and sensible. */
298 eadata = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD,
301 LASSERT(eadata != NULL);
303 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
305 CERROR("obd_unpackmd: %d\n", rc);
308 LASSERT(rc >= sizeof(*lsm));
312 GOTO(out_free_memmd, rc = -ENOMEM);
314 oa->o_id = lsm->lsm_object_id;
315 oa->o_gr = lsm->lsm_object_gr;
316 oa->o_mode = body->mode & S_IFMT;
317 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
319 if (body->valid & OBD_MD_FLCOOKIE) {
320 oa->o_valid |= OBD_MD_FLCOOKIE;
322 req_capsule_server_sized_get(&req->rq_pill,
324 sizeof(struct llog_cookie) *
325 lsm->lsm_stripe_count);
326 if (oti.oti_logcookies == NULL) {
327 oa->o_valid &= ~OBD_MD_FLCOOKIE;
328 body->valid &= ~OBD_MD_FLCOOKIE;
332 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL, NULL);
335 CERROR("obd destroy objid 0x"LPX64" error %d\n",
336 lsm->lsm_object_id, rc);
338 obd_free_memmd(llu_i2obdexp(dir), &lsm);
343 int llu_sizeonmds_update(struct inode *inode, struct lustre_handle *fh,
346 struct llu_inode_info *lli = llu_i2info(inode);
347 struct llu_sb_info *sbi = llu_i2sbi(inode);
348 struct md_op_data op_data = {{ 0 }};
349 struct obdo oa = { 0 };
353 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
354 LASSERT(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM);
356 rc = llu_inode_getattr(inode, &oa);
359 CDEBUG(D_INODE, "objid "LPX64" is already destroyed\n",
360 lli->lli_smd->lsm_object_id);
362 CERROR("inode_getattr failed (%d): unable to send a "
363 "Size-on-MDS attribute update for inode %llu/%lu\n",
364 rc, (long long)llu_i2stat(inode)->st_ino,
365 lli->lli_st_generation);
369 md_from_obdo(&op_data, &oa, oa.o_valid);
370 memcpy(&op_data.op_handle, fh, sizeof(*fh));
371 op_data.op_ioepoch = ioepoch;
372 op_data.op_flags |= MF_SOM_CHANGE;
374 rc = llu_md_setattr(inode, &op_data, NULL);
378 int llu_md_close(struct obd_export *md_exp, struct inode *inode)
380 struct llu_inode_info *lli = llu_i2info(inode);
381 struct ll_file_data *fd = lli->lli_file_data;
382 struct ptlrpc_request *req = NULL;
383 struct obd_client_handle *och = &fd->fd_mds_och;
384 struct intnl_stat *st = llu_i2stat(inode);
385 struct md_op_data op_data = { { 0 } };
389 /* clear group lock, if present */
390 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
391 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
392 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
393 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
397 op_data.op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
398 ATTR_MTIME_SET | ATTR_CTIME_SET;
400 if (fd->fd_flags & FMODE_WRITE) {
401 struct llu_sb_info *sbi = llu_i2sbi(inode);
402 if (!(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM) ||
403 !S_ISREG(llu_i2stat(inode)->st_mode)) {
404 op_data.op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
406 /* Inode cannot be dirty. Close the epoch. */
407 op_data.op_flags |= MF_EPOCH_CLOSE;
408 /* XXX: Send CHANGE flag only if Size-on-MDS inode attributes
409 * are really changed. */
410 op_data.op_flags |= MF_SOM_CHANGE;
412 /* Pack Size-on-MDS attributes if we are in IO epoch and
413 * attributes are valid. */
414 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
415 if (!cl_local_size(inode))
416 op_data.op_attr.ia_valid |=
417 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
420 op_data.op_fid1 = lli->lli_fid;
421 op_data.op_attr.ia_atime = st->st_atime;
422 op_data.op_attr.ia_mtime = st->st_mtime;
423 op_data.op_attr.ia_ctime = st->st_ctime;
424 op_data.op_attr.ia_size = st->st_size;
425 op_data.op_attr_blocks = st->st_blocks;
426 op_data.op_attr.ia_attr_flags = lli->lli_st_flags;
427 op_data.op_ioepoch = lli->lli_ioepoch;
428 memcpy(&op_data.op_handle, &och->och_fh, sizeof(op_data.op_handle));
430 rc = md_close(md_exp, &op_data, och->och_mod, &req);
432 /* We are the last writer, so the MDS has instructed us to get
433 * the file size and any write cookies, then close again. */
434 LASSERT(fd->fd_flags & FMODE_WRITE);
435 rc = llu_sizeonmds_update(inode, &och->och_fh,
438 CERROR("inode %llu mdc Size-on-MDS update failed: "
439 "rc = %d\n", (long long)st->st_ino, rc);
443 CERROR("inode %llu close failed: rc %d\n",
444 (long long)st->st_ino, rc);
446 rc = llu_objects_destroy(req, inode);
448 CERROR("inode %llu ll_objects destroy: rc = %d\n",
449 (long long)st->st_ino, rc);
452 md_clear_open_replay_data(md_exp, och);
453 ptlrpc_req_finished(req);
454 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
455 lli->lli_file_data = NULL;
456 OBD_FREE(fd, sizeof(*fd));
461 int llu_file_release(struct inode *inode)
463 struct ll_file_data *fd;
464 struct llu_sb_info *sbi = llu_i2sbi(inode);
465 struct llu_inode_info *lli = llu_i2info(inode);
469 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
470 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
472 if (llu_is_root_inode(inode))
475 /* still opened by others? */
476 if (--lli->lli_open_count)
479 fd = lli->lli_file_data;
480 if (!fd) /* no process opened the file after an mcreate */
483 rc2 = llu_md_close(sbi->ll_md_exp, inode);
491 * libsysio require us return 0
493 int llu_iop_close(struct inode *inode)
497 liblustre_wait_event(0);
499 rc = llu_file_release(inode);
501 CERROR("file close error %d\n", rc);
503 /* if open count == 0 && stale_flag is set, should we
504 * remove the inode immediately? */
505 liblustre_wait_idle();
509 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
513 liblustre_wait_event(0);
515 if (off < 0 || off > ll_file_maxbytes(ino))