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>
39 #include "llite_lib.h"
41 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
50 ll_i2uctxt(&data->ctxt, i1, i2);
51 ll_inode2fid(&data->fid1, i1);
54 ll_inode2fid(&data->fid2, i2);
58 data->namelen = namelen;
59 data->create_mode = mode;
60 data->mod_time = CURRENT_TIME;
63 void obdo_refresh_inode(struct inode *dst,
67 struct llu_inode_info *lli = llu_i2info(dst);
68 valid &= src->o_valid;
70 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
71 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
72 src->o_valid, LTIME_S(lli->lli_st_mtime),
73 LTIME_S(lli->lli_st_ctime),
74 (long)src->o_mtime, (long)src->o_ctime);
76 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
77 LTIME_S(lli->lli_st_atime) = src->o_atime;
78 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
79 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
80 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
81 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
82 if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
83 lli->lli_st_size = src->o_size;
85 if (valid & OBD_MD_FLBLKSZ)
86 lli->lli_st_blksize = src->o_blksize;
87 /* allocation of space */
88 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
89 lli->lli_st_blocks = src->o_blocks;
93 static int llu_create_obj(struct lustre_handle *conn, struct inode *inode,
94 struct lov_stripe_md *lsm)
96 struct ptlrpc_request *req = NULL;
97 struct llu_inode_info *lli = llu_i2info(inode);
98 struct lov_mds_md *lmm = NULL;
101 struct mdc_op_data op_data;
102 struct obd_trans_info oti = { 0 };
103 int rc, err, lmm_size = 0;;
110 LASSERT(S_ISREG(inode->i_mode));
111 oa->o_mode = S_IFREG | 0600;
112 oa->o_id = lli->lli_st_ino;
113 oa->o_generation = lli->lli_st_generation;
114 /* Keep these 0 for now, because chown/chgrp does not change the
115 * ownership on the OST, and we don't want to allow BA OST NFS
116 * users to access these objects by mistake.
120 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
121 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
123 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
125 (llu_i2info(inode)->lli_st_size ? OBD_MD_FLSIZE : 0));
127 rc = obd_create(conn, oa, &lsm, &oti);
129 CERROR("error creating objects for inode %lu: rc = %d\n",
130 lli->lli_st_ino, rc);
132 CERROR("obd_create returned invalid rc %d\n", rc);
137 obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
139 LASSERT(lsm && lsm->lsm_object_id);
140 rc = obd_packmd(conn, &lmm, lsm);
142 GOTO(out_destroy, rc);
146 /* Save the stripe MD with this file on the MDS */
147 memset(&iattr, 0, sizeof(iattr));
148 iattr.ia_valid = ATTR_FROM_OPEN;
150 llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
152 rc = mdc_setattr(&llu_i2sbi(inode)->ll_mdc_conn, &op_data,
153 &iattr, lmm, lmm_size, oti.oti_logcookies,
154 oti.oti_numcookies * sizeof(oti.oti_onecookie), &req);
155 ptlrpc_req_finished(req);
157 obd_free_diskmd(conn, &lmm);
159 /* If we couldn't complete mdc_open() and store the stripe MD on the
160 * MDS, we need to destroy the objects now or they will be leaked.
163 CERROR("error: storing stripe MD for %lu: rc %d\n",
164 lli->lli_st_ino, rc);
165 GOTO(out_destroy, rc);
168 lli->lli_maxbytes = lsm->lsm_maxbytes;
172 oti_free_cookies(&oti);
177 oa->o_id = lsm->lsm_object_id;
178 oa->o_valid = OBD_MD_FLID;
179 obdo_from_inode(oa, inode, OBD_MD_FLTYPE);
181 err = obd_destroy(conn, oa, lsm, NULL);
182 obd_free_memmd(conn, &lsm);
184 CERROR("error uncreating inode %lu objects: rc %d\n",
185 lli->lli_st_ino, err);
191 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
193 struct ptlrpc_request *req = it->d.lustre.it_data;
194 struct ll_file_data *fd;
195 struct mds_body *body;
198 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
199 LASSERT (body != NULL); /* reply already checked out */
200 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
202 /* already opened? */
203 if (lli->lli_open_count++)
206 LASSERT(!lli->lli_file_data);
208 OBD_ALLOC(fd, sizeof(*fd));
209 /* We can't handle this well without reorganizing ll_file_open and
210 * ll_mdc_close, so don't even try right now. */
213 memset(fd, 0, sizeof(*fd));
215 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
216 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
217 lli->lli_file_data = fd;
219 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
225 static int llu_osc_open(struct lustre_handle *conn, struct inode *inode,
226 struct lov_stripe_md *lsm)
228 struct ll_file_data *fd = llu_i2info(inode)->lli_file_data;
236 oa->o_id = lsm->lsm_object_id;
237 oa->o_mode = S_IFREG;
238 oa->o_valid = (OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLBLOCKS |
239 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
240 rc = obd_open(conn, oa, lsm, NULL, &fd->fd_ost_och);
244 /* file->f_flags &= ~O_LOV_DELAY_CREATE; */
245 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS | OBD_MD_FLMTIME |
256 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
258 struct inode *inode = pnode->p_base->pb_ino;
259 struct llu_inode_info *lli = llu_i2info(inode);
260 struct ll_file_data *fd;
261 struct ptlrpc_request *request;
262 struct lookup_intent *it;
263 struct lov_stripe_md *lsm;
267 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
268 LL_GET_INTENT(inode, it);
270 if (!it->d.lustre.it_disposition) {
272 struct lookup_intent oit = { .it_op = IT_OPEN,
273 .it_flags = file->f_flags };
275 rc = ll_intent_file_open(file, NULL, 0, it);
277 GOTO(out_release, rc);
282 rc = it_open_error(DISP_OPEN_OPEN, it);
284 GOTO(out_release, rc);
286 rc = llu_local_open(lli, it);
290 if (!S_ISREG(lli->lli_st_mode))
291 GOTO(out_release, rc = 0);
293 fd = lli->lli_file_data;
297 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
298 CDEBUG(D_INODE, "object creation was delayed\n");
299 GOTO(out_release, rc);
303 rc = llu_create_obj(conn, inode, NULL);
307 CERROR("warning: stripe already set on ino %lu\n",
313 fd->fd_flags &= ~O_LOV_DELAY_CREATE;
316 request = it->d.lustre.it_data;
317 ptlrpc_req_finished(request);
319 it->it_op_release(it);
320 OBD_FREE(it, sizeof(*it));
325 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
327 struct mds_body *body;
328 struct lov_mds_md *eadata;
329 struct lov_stripe_md *lsm = NULL;
330 struct obd_trans_info oti = { 0 };
335 /* req is swabbed so this is safe */
336 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
338 if (!(body->valid & OBD_MD_FLEASIZE))
341 if (body->eadatasize == 0) {
342 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
343 GOTO(out, rc = -EPROTO);
346 /* The MDS sent back the EA because we unlinked the last reference
347 * to this file. Use this EA to unlink the objects on the OST.
348 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
349 * check it is complete and sensible. */
350 eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
351 LASSERT(eadata != NULL);
352 if (eadata == NULL) {
353 CERROR("Can't unpack MDS EA data\n");
354 GOTO(out, rc = -EPROTO);
357 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
359 CERROR("obd_unpackmd: %d\n", rc);
362 LASSERT(rc >= sizeof(*lsm));
366 GOTO(out_free_memmd, rc = -ENOMEM);
368 oa->o_id = lsm->lsm_object_id;
369 oa->o_mode = body->mode & S_IFMT;
370 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
372 if (body->valid & OBD_MD_FLCOOKIE) {
373 oa->o_valid |= OBD_MD_FLCOOKIE;
375 lustre_msg_buf(request->rq_repmsg, 2,
376 sizeof(struct llog_cookie) *
377 lsm->lsm_stripe_count);
378 if (oti.oti_logcookies == NULL) {
379 oa->o_valid &= ~OBD_MD_FLCOOKIE;
380 body->valid &= ~OBD_MD_FLCOOKIE;
384 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
387 CERROR("obd destroy objid 0x"LPX64" error %d\n",
388 lsm->lsm_object_id, rc);
390 obd_free_memmd(llu_i2obdexp(dir), &lsm);
395 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
397 struct llu_inode_info *lli = llu_i2info(inode);
398 struct ll_file_data *fd = lli->lli_file_data;
399 struct ptlrpc_request *req = NULL;
400 struct obd_client_handle *och = &fd->fd_mds_och;
406 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
407 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
409 memset(&obdo, 0, sizeof(obdo));
410 obdo.o_id = lli->lli_st_ino;
411 obdo.o_mode = lli->lli_st_mode;
412 obdo.o_size = lli->lli_st_size;
413 obdo.o_blocks = lli->lli_st_blocks;
414 if (0 /* ll_is_inode_dirty(inode) */) {
415 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
416 valid |= OBD_MD_FLFLAGS;
418 obdo.o_valid = valid;
419 rc = mdc_close(mdc_exp, &obdo, och, &req);
421 /* We are the last writer, so the MDS has instructed us to get
422 * the file size and any write cookies, then close again. */
423 //ll_queue_done_writing(inode);
426 CERROR("inode %lu close failed: rc = %d\n", lli->lli_st_ino, rc);
428 rc = llu_objects_destroy(req, inode);
430 CERROR("inode %lu ll_objects destroy: rc = %d\n",
431 lli->lli_st_ino, rc);
434 mdc_clear_open_replay_data(och);
435 ptlrpc_req_finished(req);
436 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
437 lli->lli_file_data = NULL;
438 OBD_FREE(fd, sizeof(*fd));
443 int llu_file_release(struct inode *inode)
445 struct ll_file_data *fd;
446 struct llu_sb_info *sbi = llu_i2sbi(inode);
447 struct llu_inode_info *lli = llu_i2info(inode);
451 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
452 lli->lli_st_generation);
454 /* FIXME need add this check later. how to find the root pnode? */
456 /* don't do anything for / */
457 if (inode->i_sb->s_root == file->f_dentry)
460 /* still opened by others? */
461 if (--lli->lli_open_count)
464 fd = lli->lli_file_data;
465 if (!fd) /* no process opened the file after an mcreate */
468 rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
475 int llu_iop_close(struct inode *inode)
479 rc = llu_file_release(inode);
480 if (!llu_i2info(inode)->lli_open_count)
481 llu_i2info(inode)->lli_stale_flag = 1;
485 int llu_iop_ipreadv(struct inode *ino,
490 if (!ioctx->ioctx_iovlen)
492 if (ioctx->ioctx_iovlen < 0)
495 ioctx->ioctx_private = llu_file_read(ino,
498 ioctx->ioctx_offset);
499 if (IS_ERR(ioctx->ioctx_private))
500 return (PTR_ERR(ioctx->ioctx_private));
505 int llu_iop_ipwritev(struct inode *ino,
510 if (!ioctx->ioctx_iovlen)
512 if (ioctx->ioctx_iovlen < 0)
515 ioctx->ioctx_private = llu_file_write(ino,
518 ioctx->ioctx_offset);
519 if (IS_ERR(ioctx->ioctx_private))
520 return (PTR_ERR(ioctx->ioctx_private));
525 /* this isn't where truncate starts. roughly:
526 * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
527 * we grab the lock back in setattr_raw to avoid races. */
528 static void llu_truncate(struct inode *inode)
530 struct llu_inode_info *lli = llu_i2info(inode);
531 struct lov_stripe_md *lsm = lli->lli_smd;
532 struct obdo oa = {0};
535 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
536 lli->lli_st_generation);
539 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
544 oa.o_id = lsm->lsm_object_id;
545 oa.o_valid = OBD_MD_FLID;
546 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
547 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
549 CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
550 oa.o_id, lli->lli_st_size);
552 /* truncate == punch from new size to absolute end of file */
553 err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
554 OBD_OBJECT_EOF, NULL);
556 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
558 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
559 OBD_MD_FLATIME | OBD_MD_FLMTIME |
566 int llu_vmtruncate(struct inode * inode, loff_t offset)
568 struct llu_inode_info *lli = llu_i2info(inode);
570 lli->lli_st_size = offset;