1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Lite routines to issue a secondary close after writeback
6 * Copyright (c) 2001-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 #include <linux/module.h>
26 #define DEBUG_SUBSYSTEM S_LLITE
28 #include <lustre_lite.h>
29 #include "llite_internal.h"
31 /* record that a write is in flight */
32 void llap_write_pending(struct inode *inode, struct ll_async_page *llap)
34 struct ll_inode_info *lli = ll_i2info(inode);
35 spin_lock(&lli->lli_lock);
36 list_add(&llap->llap_pending_write, &lli->lli_pending_write_llaps);
37 spin_unlock(&lli->lli_lock);
40 /* record that a write has completed */
41 void llap_write_complete(struct inode *inode, struct ll_async_page *llap)
43 struct ll_inode_info *lli = ll_i2info(inode);
44 spin_lock(&lli->lli_lock);
45 list_del_init(&llap->llap_pending_write);
46 spin_unlock(&lli->lli_lock);
49 void ll_open_complete(struct inode *inode)
51 struct ll_inode_info *lli = ll_i2info(inode);
52 spin_lock(&lli->lli_lock);
53 lli->lli_send_done_writing = 0;
54 spin_unlock(&lli->lli_lock);
57 /* if we close with writes in flight then we want the completion or cancelation
58 * of those writes to send a DONE_WRITING rpc to the MDS */
59 int ll_is_inode_dirty(struct inode *inode)
61 struct ll_inode_info *lli = ll_i2info(inode);
65 spin_lock(&lli->lli_lock);
66 if (!list_empty(&lli->lli_pending_write_llaps))
68 spin_unlock(&lli->lli_lock);
72 void ll_try_done_writing(struct inode *inode)
74 struct ll_inode_info *lli = ll_i2info(inode);
75 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
77 spin_lock(&lli->lli_lock);
79 if (lli->lli_send_done_writing &&
80 list_empty(&lli->lli_pending_write_llaps)) {
82 spin_lock(&lcq->lcq_lock);
83 if (list_empty(&lli->lli_close_item)) {
84 CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
85 inode->i_ino, inode->i_generation);
87 list_add_tail(&lli->lli_close_item, &lcq->lcq_list);
88 wake_up(&lcq->lcq_waitq);
90 spin_unlock(&lcq->lcq_lock);
93 spin_unlock(&lli->lli_lock);
96 /* The MDS needs us to get the real file attributes, then send a DONE_WRITING */
97 void ll_queue_done_writing(struct inode *inode)
99 struct ll_inode_info *lli = ll_i2info(inode);
102 spin_lock(&lli->lli_lock);
103 lli->lli_send_done_writing = 1;
104 spin_unlock(&lli->lli_lock);
106 ll_try_done_writing(inode);
111 /* If we know the file size and have the cookies:
112 * - send a DONE_WRITING rpc
115 * - get a whole-file lock
116 * - get the authoritative size and all cookies with GETATTRs
117 * - send a DONE_WRITING rpc
119 static void ll_close_done_writing(struct inode *inode)
121 struct ll_inode_info *lli = ll_i2info(inode);
122 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF } };
123 struct lustre_handle lockh = { 0 };
126 int rc, ast_flags = 0;
129 memset(&obdo, 0, sizeof(obdo));
130 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
133 rc = ll_extent_lock(NULL, inode, lli->lli_smd, LCK_PW, &policy, &lockh,
136 CERROR("lock acquisition failed (%d): unable to send "
137 "DONE_WRITING for inode %lu/%u\n", rc, inode->i_ino,
138 inode->i_generation);
142 rc = ll_lsm_getattr(ll_i2obdexp(inode), lli->lli_smd, &obdo);
144 CERROR("inode_getattr failed (%d): unable to send DONE_WRITING "
145 "for inode %lu/%u\n", rc, inode->i_ino,
146 inode->i_generation);
147 ll_extent_unlock(NULL, inode, lli->lli_smd, LCK_PW, &lockh);
151 obdo_refresh_inode(inode, &obdo, valid);
153 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
154 lli->lli_smd->lsm_object_id, inode->i_size, inode->i_blocks,
155 1<<inode->i_blkbits);
157 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
159 rc = ll_extent_unlock(NULL, inode, lli->lli_smd, LCK_PW, &lockh);
161 CERROR("unlock failed (%d)? proceeding anyways...\n", rc);
164 obdo.o_id = inode->i_ino;
165 obdo.o_size = inode->i_size;
166 obdo.o_blocks = inode->i_blocks;
167 obdo.o_valid = OBD_MD_FLID | OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
169 rc = mdc_done_writing(ll_i2sbi(inode)->ll_mdc_exp, &obdo);
174 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
176 struct ll_inode_info *lli = NULL;
178 spin_lock(&lcq->lcq_lock);
180 if (lcq->lcq_list.next == NULL)
182 else if (!list_empty(&lcq->lcq_list)) {
183 lli = list_entry(lcq->lcq_list.next, struct ll_inode_info,
185 list_del(&lli->lli_close_item);
188 spin_unlock(&lcq->lcq_lock);
192 static int ll_close_thread(void *arg)
194 struct ll_close_queue *lcq = arg;
198 char name[CFS_CURPROC_COMM_MAX];
199 snprintf(name, sizeof(name) - 1, "ll_close");
203 complete(&lcq->lcq_comp);
206 struct l_wait_info lwi = { 0 };
207 struct ll_inode_info *lli;
208 //struct inode *inode;
210 l_wait_event_exclusive(lcq->lcq_waitq,
211 (lli = ll_close_next_lli(lcq)) != NULL,
216 //inode = ll_info2i(lli);
217 //ll_close_done_writing(inode);
221 complete(&lcq->lcq_comp);
225 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
227 struct ll_close_queue *lcq;
230 OBD_ALLOC(lcq, sizeof(*lcq));
234 spin_lock_init(&lcq->lcq_lock);
235 INIT_LIST_HEAD(&lcq->lcq_list);
236 init_waitqueue_head(&lcq->lcq_waitq);
237 init_completion(&lcq->lcq_comp);
239 pid = kernel_thread(ll_close_thread, lcq, 0);
241 OBD_FREE(lcq, sizeof(*lcq));
245 wait_for_completion(&lcq->lcq_comp);
250 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
252 init_completion(&lcq->lcq_comp);
253 lcq->lcq_list.next = NULL;
254 wake_up(&lcq->lcq_waitq);
255 wait_for_completion(&lcq->lcq_comp);
256 OBD_FREE(lcq, sizeof(*lcq));