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/llite/llite_close.c
38 * Lustre Lite routines to issue a secondary close after writeback
41 #include <linux/module.h>
43 #define DEBUG_SUBSYSTEM S_LLITE
45 //#include <lustre_mdc.h>
46 #include <lustre_lite.h>
47 #include "llite_internal.h"
49 /** records that a write is in flight */
50 void vvp_write_pending(struct ccc_object *club, struct ccc_page *page)
52 struct ll_inode_info *lli = ll_i2info(club->cob_inode);
55 spin_lock(&lli->lli_lock);
56 lli->lli_flags |= LLIF_SOM_DIRTY;
57 if (page != NULL && list_empty(&page->cpg_pending_linkage))
58 list_add(&page->cpg_pending_linkage, &club->cob_pending_list);
59 spin_unlock(&lli->lli_lock);
63 /** records that a write has completed */
64 void vvp_write_complete(struct ccc_object *club, struct ccc_page *page)
66 struct ll_inode_info *lli = ll_i2info(club->cob_inode);
70 spin_lock(&lli->lli_lock);
71 if (page != NULL && !list_empty(&page->cpg_pending_linkage)) {
72 list_del_init(&page->cpg_pending_linkage);
75 spin_unlock(&lli->lli_lock);
77 ll_queue_done_writing(club->cob_inode, 0);
81 /** Queues DONE_WRITING if
82 * - done writing is allowed;
83 * - inode has no no dirty pages; */
84 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
86 struct ll_inode_info *lli = ll_i2info(inode);
87 struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
90 spin_lock(&lli->lli_lock);
91 lli->lli_flags |= flags;
93 if ((lli->lli_flags & LLIF_DONE_WRITING) &&
94 list_empty(&club->cob_pending_list)) {
95 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
97 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
98 CWARN("ino %lu/%u(flags %lu) som valid it just after "
100 inode->i_ino, inode->i_generation,
102 /* DONE_WRITING is allowed and inode has no dirty page. */
103 spin_lock(&lcq->lcq_lock);
105 LASSERT(list_empty(&lli->lli_close_list));
106 CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
107 inode->i_ino, inode->i_generation);
108 list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
110 /* Avoid a concurrent insertion into the close thread queue:
111 * an inode is already in the close thread, open(), write(),
112 * close() happen, epoch is closed as the inode is marked as
113 * LLIF_EPOCH_PENDING. When pages are written inode should not
114 * be inserted into the queue again, clear this flag to avoid
116 lli->lli_flags &= ~LLIF_DONE_WRITING;
118 wake_up(&lcq->lcq_waitq);
119 spin_unlock(&lcq->lcq_lock);
121 spin_unlock(&lli->lli_lock);
125 /** Closes epoch and sends Size-on-MDS attribute update if possible. Call
126 * this under ll_inode_info::lli_lock spinlock. */
127 void ll_epoch_close(struct inode *inode, struct md_op_data *op_data,
128 struct obd_client_handle **och, unsigned long flags)
130 struct ll_inode_info *lli = ll_i2info(inode);
131 struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
134 spin_lock(&lli->lli_lock);
135 if (!(list_empty(&club->cob_pending_list))) {
136 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
137 LASSERT(*och != NULL);
138 LASSERT(lli->lli_pending_och == NULL);
139 /* Inode is dirty and there is no pending write done
140 * request yet, DONE_WRITE is to be sent later. */
141 lli->lli_flags |= LLIF_EPOCH_PENDING;
142 lli->lli_pending_och = *och;
143 spin_unlock(&lli->lli_lock);
145 inode = igrab(inode);
149 if (flags & LLIF_DONE_WRITING) {
150 /* Some pages are still dirty, it is early to send
151 * DONE_WRITE. Wait untill all pages will be flushed
152 * and try DONE_WRITE again later. */
153 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
154 lli->lli_flags |= LLIF_DONE_WRITING;
155 spin_unlock(&lli->lli_lock);
157 inode = igrab(inode);
162 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
163 ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
164 op_data->op_flags |= MF_EPOCH_CLOSE;
166 if (flags & LLIF_DONE_WRITING) {
167 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
168 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
169 *och = lli->lli_pending_och;
170 lli->lli_pending_och = NULL;
171 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
173 /* Pack Size-on-MDS inode attributes only if they has changed */
174 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
175 spin_unlock(&lli->lli_lock);
179 /* There is a pending DONE_WRITE -- close epoch with no
180 * attribute change. */
181 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
182 spin_unlock(&lli->lli_lock);
187 LASSERT(list_empty(&club->cob_pending_list));
188 lli->lli_flags &= ~LLIF_SOM_DIRTY;
189 spin_unlock(&lli->lli_lock);
190 op_data->op_flags |= MF_SOM_CHANGE;
192 /* Check if Size-on-MDS attributes are valid. */
193 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
194 CWARN("ino %lu/%u(flags %lu) som valid it just after "
196 inode->i_ino, inode->i_generation, lli->lli_flags);
198 if (!cl_local_size(inode)) {
199 /* Send Size-on-MDS Attributes if valid. Atime is sent along
200 * with all the attributes. */
201 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
202 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
209 int ll_sizeonmds_update(struct inode *inode, struct lustre_handle *fh,
212 struct ll_inode_info *lli = ll_i2info(inode);
213 struct md_op_data *op_data;
218 /* LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)); */
219 /* After recovery that can be valid. */
220 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
221 CWARN("ino %lu/%u(flags %lu) som valid it just after "
222 "recovery\n", inode->i_ino, inode->i_generation,
226 OBD_ALLOC_PTR(op_data);
227 if (!oa || !op_data) {
228 CERROR("can't allocate memory for Size-on-MDS update.\n");
231 rc = ll_inode_getattr(inode, oa);
234 CDEBUG(D_INODE, "objid "LPX64" is already destroyed\n",
235 lli->lli_smd->lsm_object_id);
237 CERROR("inode_getattr failed (%d): unable to send a "
238 "Size-on-MDS attribute update for inode %lu/%u\n",
239 rc, inode->i_ino, inode->i_generation);
242 CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n", PFID(&lli->lli_fid));
244 md_from_obdo(op_data, oa, oa->o_valid);
245 memcpy(&op_data->op_handle, fh, sizeof(*fh));
247 op_data->op_ioepoch = ioepoch;
248 op_data->op_flags |= MF_SOM_CHANGE;
250 rc = ll_md_setattr(inode, op_data, NULL);
256 ll_finish_md_op_data(op_data);
260 /** Sends a DONE_WRITING rpc, packs Size-on-MDS attributes into it, if
262 static void ll_done_writing(struct inode *inode)
264 struct obd_client_handle *och = NULL;
265 struct md_op_data *op_data;
269 LASSERT(ll_i2mdexp(inode)->exp_connect_flags & OBD_CONNECT_SOM);
271 OBD_ALLOC_PTR(op_data);
272 if (op_data == NULL) {
273 CERROR("can't allocate op_data\n");
278 ll_epoch_close(inode, op_data, &och, LLIF_DONE_WRITING);
279 /* If there is no @och, we do not do D_W yet. */
283 ll_pack_inode2opdata(inode, op_data, &och->och_fh);
285 rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
287 /* MDS has instructed us to obtain Size-on-MDS attribute from
288 * OSTs and send setattr to back to MDS. */
289 rc = ll_sizeonmds_update(inode, &och->och_fh,
290 op_data->op_ioepoch);
292 CERROR("inode %lu mdc done_writing failed: rc = %d\n",
296 ll_finish_md_op_data(op_data);
298 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
304 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
306 struct ll_inode_info *lli = NULL;
308 spin_lock(&lcq->lcq_lock);
310 if (!list_empty(&lcq->lcq_head)) {
311 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
313 list_del_init(&lli->lli_close_list);
314 } else if (atomic_read(&lcq->lcq_stop))
315 lli = ERR_PTR(-EALREADY);
317 spin_unlock(&lcq->lcq_lock);
321 static int ll_close_thread(void *arg)
323 struct ll_close_queue *lcq = arg;
327 char name[CFS_CURPROC_COMM_MAX];
328 snprintf(name, sizeof(name) - 1, "ll_close");
332 complete(&lcq->lcq_comp);
335 struct l_wait_info lwi = { 0 };
336 struct ll_inode_info *lli;
339 l_wait_event_exclusive(lcq->lcq_waitq,
340 (lli = ll_close_next_lli(lcq)) != NULL,
345 inode = ll_info2i(lli);
346 CDEBUG(D_INFO, "done_writting for inode %lu/%u\n",
347 inode->i_ino, inode->i_generation);
348 ll_done_writing(inode);
352 CDEBUG(D_INFO, "ll_close exiting\n");
353 complete(&lcq->lcq_comp);
357 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
359 struct ll_close_queue *lcq;
362 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
365 OBD_ALLOC(lcq, sizeof(*lcq));
369 spin_lock_init(&lcq->lcq_lock);
370 INIT_LIST_HEAD(&lcq->lcq_head);
371 init_waitqueue_head(&lcq->lcq_waitq);
372 init_completion(&lcq->lcq_comp);
374 pid = kernel_thread(ll_close_thread, lcq, 0);
376 OBD_FREE(lcq, sizeof(*lcq));
380 wait_for_completion(&lcq->lcq_comp);
385 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
387 init_completion(&lcq->lcq_comp);
388 atomic_inc(&lcq->lcq_stop);
389 wake_up(&lcq->lcq_waitq);
390 wait_for_completion(&lcq->lcq_comp);
391 OBD_FREE(lcq, sizeof(*lcq));