4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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_lite.h>
46 #include "llite_internal.h"
48 /** records that a write is in flight */
49 void vvp_write_pending(struct ccc_object *club, struct ccc_page *page)
51 struct ll_inode_info *lli = ll_i2info(club->cob_inode);
54 spin_lock(&lli->lli_lock);
55 lli->lli_flags |= LLIF_SOM_DIRTY;
56 if (page != NULL && cfs_list_empty(&page->cpg_pending_linkage))
57 cfs_list_add(&page->cpg_pending_linkage,
58 &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 && !cfs_list_empty(&page->cpg_pending_linkage)) {
72 cfs_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 cfs_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("%s: file "DFID"(flags %u) Size-on-MDS valid, "
99 "done writing allowed and no diry pages\n",
100 ll_get_fsname(inode->i_sb, NULL, 0),
101 PFID(ll_inode2fid(inode)), lli->lli_flags);
102 /* DONE_WRITING is allowed and inode has no dirty page. */
103 spin_lock(&lcq->lcq_lock);
105 LASSERT(cfs_list_empty(&lli->lli_close_list));
106 CDEBUG(D_INODE, "adding inode "DFID" to close list\n",
107 PFID(ll_inode2fid(inode)));
108 cfs_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 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
126 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
128 struct ll_inode_info *lli = ll_i2info(inode);
131 op_data->op_flags |= MF_SOM_CHANGE;
132 /* Check if Size-on-MDS attributes are valid. */
133 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
134 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on "
135 "Size-on-MDS attributes\n",
136 ll_get_fsname(inode->i_sb, NULL, 0),
137 PFID(ll_inode2fid(inode)), lli->lli_flags);
139 if (!cl_local_size(inode)) {
140 /* Send Size-on-MDS Attributes if valid. */
141 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
142 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
147 /** Closes ioepoch and packs Size-on-MDS attribute if needed into @op_data. */
148 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
149 struct obd_client_handle **och, unsigned long flags)
151 struct ll_inode_info *lli = ll_i2info(inode);
152 struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
155 spin_lock(&lli->lli_lock);
156 if (!(cfs_list_empty(&club->cob_pending_list))) {
157 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
158 LASSERT(*och != NULL);
159 LASSERT(lli->lli_pending_och == NULL);
160 /* Inode is dirty and there is no pending write done
161 * request yet, DONE_WRITE is to be sent later. */
162 lli->lli_flags |= LLIF_EPOCH_PENDING;
163 lli->lli_pending_och = *och;
164 spin_unlock(&lli->lli_lock);
166 inode = igrab(inode);
170 if (flags & LLIF_DONE_WRITING) {
171 /* Some pages are still dirty, it is early to send
172 * DONE_WRITE. Wait untill all pages will be flushed
173 * and try DONE_WRITE again later. */
174 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
175 lli->lli_flags |= LLIF_DONE_WRITING;
176 spin_unlock(&lli->lli_lock);
178 inode = igrab(inode);
183 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
184 ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
185 op_data->op_flags |= MF_EPOCH_CLOSE;
187 if (flags & LLIF_DONE_WRITING) {
188 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
189 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
190 *och = lli->lli_pending_och;
191 lli->lli_pending_och = NULL;
192 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
194 /* Pack Size-on-MDS inode attributes only if they has changed */
195 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
196 spin_unlock(&lli->lli_lock);
200 /* There is a pending DONE_WRITE -- close epoch with no
201 * attribute change. */
202 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
203 spin_unlock(&lli->lli_lock);
208 LASSERT(cfs_list_empty(&club->cob_pending_list));
209 lli->lli_flags &= ~LLIF_SOM_DIRTY;
210 spin_unlock(&lli->lli_lock);
211 ll_done_writing_attr(inode, op_data);
219 * Cliens updates SOM attributes on MDS (including llog cookies):
220 * obd_getattr with no lock and md_setattr.
222 int ll_som_update(struct inode *inode, struct md_op_data *op_data)
224 struct ll_inode_info *lli = ll_i2info(inode);
225 struct ptlrpc_request *request = NULL;
231 LASSERT(op_data != NULL);
232 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
233 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on "
234 "Size-on-MDS attributes\n",
235 ll_get_fsname(inode->i_sb, NULL, 0),
236 PFID(ll_inode2fid(inode)), lli->lli_flags);
240 CERROR("can't allocate memory for Size-on-MDS update.\n");
244 old_flags = op_data->op_flags;
245 op_data->op_flags = MF_SOM_CHANGE;
247 /* If inode is already in another epoch, skip getattr from OSTs. */
248 if (lli->lli_ioepoch == op_data->op_ioepoch) {
249 rc = ll_inode_getattr(inode, oa, op_data->op_ioepoch,
250 old_flags & MF_GETATTR_LOCK);
254 CERROR("%s: inode_getattr failed - unable to "
255 "send a Size-on-MDS attribute update "
256 "for inode "DFID": rc = %d\n",
257 ll_get_fsname(inode->i_sb, NULL, 0),
258 PFID(ll_inode2fid(inode)), rc);
260 CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
261 PFID(&lli->lli_fid));
263 /* Install attributes into op_data. */
264 md_from_obdo(op_data, oa, oa->o_valid);
267 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data,
268 NULL, 0, NULL, 0, &request, NULL);
269 ptlrpc_req_finished(request);
276 * Closes the ioepoch and packs all the attributes into @op_data for
279 static void ll_prepare_done_writing(struct inode *inode,
280 struct md_op_data *op_data,
281 struct obd_client_handle **och)
283 ll_ioepoch_close(inode, op_data, och, LLIF_DONE_WRITING);
284 /* If there is no @och, we do not do D_W yet. */
288 ll_pack_inode2opdata(inode, op_data, &(*och)->och_fh);
289 ll_prep_md_op_data(op_data, inode, NULL, NULL,
290 0, 0, LUSTRE_OPC_ANY, NULL);
293 /** Send a DONE_WRITING rpc. */
294 static void ll_done_writing(struct inode *inode)
296 struct obd_client_handle *och = NULL;
297 struct md_op_data *op_data;
301 LASSERT(exp_connect_som(ll_i2mdexp(inode)));
303 OBD_ALLOC_PTR(op_data);
304 if (op_data == NULL) {
305 CERROR("can't allocate op_data\n");
310 ll_prepare_done_writing(inode, op_data, &och);
311 /* If there is no @och, we do not do D_W yet. */
315 rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
317 /* MDS has instructed us to obtain Size-on-MDS attribute from
318 * OSTs and send setattr to back to MDS. */
319 rc = ll_som_update(inode, op_data);
321 CERROR("%s: inode "DFID" mdc done_writing failed: rc = %d\n",
322 ll_get_fsname(inode->i_sb, NULL, 0),
323 PFID(ll_inode2fid(inode)), rc);
326 ll_finish_md_op_data(op_data);
328 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
334 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
336 struct ll_inode_info *lli = NULL;
338 spin_lock(&lcq->lcq_lock);
340 if (!cfs_list_empty(&lcq->lcq_head)) {
341 lli = cfs_list_entry(lcq->lcq_head.next, struct ll_inode_info,
343 cfs_list_del_init(&lli->lli_close_list);
344 } else if (cfs_atomic_read(&lcq->lcq_stop))
345 lli = ERR_PTR(-EALREADY);
347 spin_unlock(&lcq->lcq_lock);
351 static int ll_close_thread(void *arg)
353 struct ll_close_queue *lcq = arg;
356 complete(&lcq->lcq_comp);
359 struct l_wait_info lwi = { 0 };
360 struct ll_inode_info *lli;
363 l_wait_event_exclusive(lcq->lcq_waitq,
364 (lli = ll_close_next_lli(lcq)) != NULL,
369 inode = ll_info2i(lli);
370 CDEBUG(D_INFO, "done_writting for inode "DFID"\n",
371 PFID(ll_inode2fid(inode)));
372 ll_done_writing(inode);
376 CDEBUG(D_INFO, "ll_close exiting\n");
377 complete(&lcq->lcq_comp);
381 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
383 struct ll_close_queue *lcq;
384 struct task_struct *task;
386 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
389 OBD_ALLOC(lcq, sizeof(*lcq));
393 spin_lock_init(&lcq->lcq_lock);
394 CFS_INIT_LIST_HEAD(&lcq->lcq_head);
395 init_waitqueue_head(&lcq->lcq_waitq);
396 init_completion(&lcq->lcq_comp);
398 task = kthread_run(ll_close_thread, lcq, "ll_close");
400 OBD_FREE(lcq, sizeof(*lcq));
401 return PTR_ERR(task);
404 wait_for_completion(&lcq->lcq_comp);
409 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
411 init_completion(&lcq->lcq_comp);
412 cfs_atomic_inc(&lcq->lcq_stop);
413 wake_up(&lcq->lcq_waitq);
414 wait_for_completion(&lcq->lcq_comp);
415 OBD_FREE(lcq, sizeof(*lcq));