• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/llite_close.c
37  *
38  * Lustre Lite routines to issue a secondary close after writeback
39  */
40 
41 #include <linux/module.h>
42 
43 #define DEBUG_SUBSYSTEM S_LLITE
44 
45 #include "../include/lustre_lite.h"
46 #include "llite_internal.h"
47 
48 /** records that a write is in flight */
vvp_write_pending(struct ccc_object * club,struct ccc_page * page)49 void vvp_write_pending(struct ccc_object *club, struct ccc_page *page)
50 {
51 	struct ll_inode_info *lli = ll_i2info(club->cob_inode);
52 
53 	spin_lock(&lli->lli_lock);
54 	lli->lli_flags |= LLIF_SOM_DIRTY;
55 	if (page != NULL && list_empty(&page->cpg_pending_linkage))
56 		list_add(&page->cpg_pending_linkage,
57 			     &club->cob_pending_list);
58 	spin_unlock(&lli->lli_lock);
59 }
60 
61 /** records that a write has completed */
vvp_write_complete(struct ccc_object * club,struct ccc_page * page)62 void vvp_write_complete(struct ccc_object *club, struct ccc_page *page)
63 {
64 	struct ll_inode_info *lli = ll_i2info(club->cob_inode);
65 	int rc = 0;
66 
67 	spin_lock(&lli->lli_lock);
68 	if (page != NULL && !list_empty(&page->cpg_pending_linkage)) {
69 		list_del_init(&page->cpg_pending_linkage);
70 		rc = 1;
71 	}
72 	spin_unlock(&lli->lli_lock);
73 	if (rc)
74 		ll_queue_done_writing(club->cob_inode, 0);
75 }
76 
77 /** Queues DONE_WRITING if
78  * - done writing is allowed;
79  * - inode has no no dirty pages; */
ll_queue_done_writing(struct inode * inode,unsigned long flags)80 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
81 {
82 	struct ll_inode_info *lli = ll_i2info(inode);
83 	struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
84 
85 	spin_lock(&lli->lli_lock);
86 	lli->lli_flags |= flags;
87 
88 	if ((lli->lli_flags & LLIF_DONE_WRITING) &&
89 	    list_empty(&club->cob_pending_list)) {
90 		struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
91 
92 		if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
93 			CWARN("ino %lu/%u(flags %u) som valid it just after recovery\n",
94 			      inode->i_ino, inode->i_generation,
95 			      lli->lli_flags);
96 		/* DONE_WRITING is allowed and inode has no dirty page. */
97 		spin_lock(&lcq->lcq_lock);
98 
99 		LASSERT(list_empty(&lli->lli_close_list));
100 		CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
101 		       inode->i_ino, inode->i_generation);
102 		list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
103 
104 		/* Avoid a concurrent insertion into the close thread queue:
105 		 * an inode is already in the close thread, open(), write(),
106 		 * close() happen, epoch is closed as the inode is marked as
107 		 * LLIF_EPOCH_PENDING. When pages are written inode should not
108 		 * be inserted into the queue again, clear this flag to avoid
109 		 * it. */
110 		lli->lli_flags &= ~LLIF_DONE_WRITING;
111 
112 		wake_up(&lcq->lcq_waitq);
113 		spin_unlock(&lcq->lcq_lock);
114 	}
115 	spin_unlock(&lli->lli_lock);
116 }
117 
118 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
ll_done_writing_attr(struct inode * inode,struct md_op_data * op_data)119 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
120 {
121 	struct ll_inode_info *lli = ll_i2info(inode);
122 
123 	op_data->op_flags |= MF_SOM_CHANGE;
124 	/* Check if Size-on-MDS attributes are valid. */
125 	if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
126 		CERROR("ino %lu/%u(flags %u) som valid it just after recovery\n",
127 		       inode->i_ino, inode->i_generation,
128 		       lli->lli_flags);
129 
130 	if (!cl_local_size(inode)) {
131 		/* Send Size-on-MDS Attributes if valid. */
132 		op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
133 				ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
134 	}
135 }
136 
137 /** Closes ioepoch and packs Size-on-MDS attribute if needed into @op_data. */
ll_ioepoch_close(struct inode * inode,struct md_op_data * op_data,struct obd_client_handle ** och,unsigned long flags)138 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
139 		      struct obd_client_handle **och, unsigned long flags)
140 {
141 	struct ll_inode_info *lli = ll_i2info(inode);
142 	struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
143 
144 	spin_lock(&lli->lli_lock);
145 	if (!(list_empty(&club->cob_pending_list))) {
146 		if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
147 			LASSERT(*och != NULL);
148 			LASSERT(lli->lli_pending_och == NULL);
149 			/* Inode is dirty and there is no pending write done
150 			 * request yet, DONE_WRITE is to be sent later. */
151 			lli->lli_flags |= LLIF_EPOCH_PENDING;
152 			lli->lli_pending_och = *och;
153 			spin_unlock(&lli->lli_lock);
154 
155 			inode = igrab(inode);
156 			LASSERT(inode);
157 			goto out;
158 		}
159 		if (flags & LLIF_DONE_WRITING) {
160 			/* Some pages are still dirty, it is early to send
161 			 * DONE_WRITE. Wait until all pages will be flushed
162 			 * and try DONE_WRITE again later. */
163 			LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
164 			lli->lli_flags |= LLIF_DONE_WRITING;
165 			spin_unlock(&lli->lli_lock);
166 
167 			inode = igrab(inode);
168 			LASSERT(inode);
169 			goto out;
170 		}
171 	}
172 	CDEBUG(D_INODE, "Epoch %llu closed on "DFID"\n",
173 	       ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
174 	op_data->op_flags |= MF_EPOCH_CLOSE;
175 
176 	if (flags & LLIF_DONE_WRITING) {
177 		LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
178 		LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
179 		*och = lli->lli_pending_och;
180 		lli->lli_pending_och = NULL;
181 		lli->lli_flags &= ~LLIF_EPOCH_PENDING;
182 	} else {
183 		/* Pack Size-on-MDS inode attributes only if they has changed */
184 		if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
185 			spin_unlock(&lli->lli_lock);
186 			goto out;
187 		}
188 
189 		/* There is a pending DONE_WRITE -- close epoch with no
190 		 * attribute change. */
191 		if (lli->lli_flags & LLIF_EPOCH_PENDING) {
192 			spin_unlock(&lli->lli_lock);
193 			goto out;
194 		}
195 	}
196 
197 	LASSERT(list_empty(&club->cob_pending_list));
198 	lli->lli_flags &= ~LLIF_SOM_DIRTY;
199 	spin_unlock(&lli->lli_lock);
200 	ll_done_writing_attr(inode, op_data);
201 
202 out:
203 	return;
204 }
205 
206 /**
207  * Cliens updates SOM attributes on MDS (including llog cookies):
208  * obd_getattr with no lock and md_setattr.
209  */
ll_som_update(struct inode * inode,struct md_op_data * op_data)210 int ll_som_update(struct inode *inode, struct md_op_data *op_data)
211 {
212 	struct ll_inode_info *lli = ll_i2info(inode);
213 	struct ptlrpc_request *request = NULL;
214 	__u32 old_flags;
215 	struct obdo *oa;
216 	int rc;
217 
218 	LASSERT(op_data != NULL);
219 	if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
220 		CERROR("ino %lu/%u(flags %u) som valid it just after recovery\n",
221 		       inode->i_ino, inode->i_generation,
222 		       lli->lli_flags);
223 
224 	oa = kmem_cache_alloc(obdo_cachep, GFP_NOFS | __GFP_ZERO);
225 	if (!oa) {
226 		CERROR("can't allocate memory for Size-on-MDS update.\n");
227 		return -ENOMEM;
228 	}
229 
230 	old_flags = op_data->op_flags;
231 	op_data->op_flags = MF_SOM_CHANGE;
232 
233 	/* If inode is already in another epoch, skip getattr from OSTs. */
234 	if (lli->lli_ioepoch == op_data->op_ioepoch) {
235 		rc = ll_inode_getattr(inode, oa, op_data->op_ioepoch,
236 				      old_flags & MF_GETATTR_LOCK);
237 		if (rc) {
238 			oa->o_valid = 0;
239 			if (rc != -ENOENT)
240 				CERROR("inode_getattr failed (%d): unable to send a Size-on-MDS attribute update for inode %lu/%u\n",
241 				       rc, inode->i_ino,
242 				       inode->i_generation);
243 		} else {
244 			CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
245 			       PFID(&lli->lli_fid));
246 		}
247 		/* Install attributes into op_data. */
248 		md_from_obdo(op_data, oa, oa->o_valid);
249 	}
250 
251 	rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data,
252 			NULL, 0, NULL, 0, &request, NULL);
253 	ptlrpc_req_finished(request);
254 
255 	kmem_cache_free(obdo_cachep, oa);
256 	return rc;
257 }
258 
259 /**
260  * Closes the ioepoch and packs all the attributes into @op_data for
261  * DONE_WRITING rpc.
262  */
ll_prepare_done_writing(struct inode * inode,struct md_op_data * op_data,struct obd_client_handle ** och)263 static void ll_prepare_done_writing(struct inode *inode,
264 				    struct md_op_data *op_data,
265 				    struct obd_client_handle **och)
266 {
267 	ll_ioepoch_close(inode, op_data, och, LLIF_DONE_WRITING);
268 	/* If there is no @och, we do not do D_W yet. */
269 	if (*och == NULL)
270 		return;
271 
272 	ll_pack_inode2opdata(inode, op_data, &(*och)->och_fh);
273 	ll_prep_md_op_data(op_data, inode, NULL, NULL,
274 			   0, 0, LUSTRE_OPC_ANY, NULL);
275 }
276 
277 /** Send a DONE_WRITING rpc. */
ll_done_writing(struct inode * inode)278 static void ll_done_writing(struct inode *inode)
279 {
280 	struct obd_client_handle *och = NULL;
281 	struct md_op_data *op_data;
282 	int rc;
283 
284 	LASSERT(exp_connect_som(ll_i2mdexp(inode)));
285 
286 	op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
287 	if (!op_data)
288 		return;
289 
290 	ll_prepare_done_writing(inode, op_data, &och);
291 	/* If there is no @och, we do not do D_W yet. */
292 	if (och == NULL)
293 		goto out;
294 
295 	rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
296 	if (rc == -EAGAIN)
297 		/* MDS has instructed us to obtain Size-on-MDS attribute from
298 		 * OSTs and send setattr to back to MDS. */
299 		rc = ll_som_update(inode, op_data);
300 	else if (rc)
301 		CERROR("inode %lu mdc done_writing failed: rc = %d\n",
302 		       inode->i_ino, rc);
303 out:
304 	ll_finish_md_op_data(op_data);
305 	if (och) {
306 		md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
307 		kfree(och);
308 	}
309 }
310 
ll_close_next_lli(struct ll_close_queue * lcq)311 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
312 {
313 	struct ll_inode_info *lli = NULL;
314 
315 	spin_lock(&lcq->lcq_lock);
316 
317 	if (!list_empty(&lcq->lcq_head)) {
318 		lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
319 				     lli_close_list);
320 		list_del_init(&lli->lli_close_list);
321 	} else if (atomic_read(&lcq->lcq_stop))
322 		lli = ERR_PTR(-EALREADY);
323 
324 	spin_unlock(&lcq->lcq_lock);
325 	return lli;
326 }
327 
ll_close_thread(void * arg)328 static int ll_close_thread(void *arg)
329 {
330 	struct ll_close_queue *lcq = arg;
331 
332 	complete(&lcq->lcq_comp);
333 
334 	while (1) {
335 		struct l_wait_info lwi = { 0 };
336 		struct ll_inode_info *lli;
337 		struct inode *inode;
338 
339 		l_wait_event_exclusive(lcq->lcq_waitq,
340 				       (lli = ll_close_next_lli(lcq)) != NULL,
341 				       &lwi);
342 		if (IS_ERR(lli))
343 			break;
344 
345 		inode = ll_info2i(lli);
346 		CDEBUG(D_INFO, "done_writing for inode %lu/%u\n",
347 		       inode->i_ino, inode->i_generation);
348 		ll_done_writing(inode);
349 		iput(inode);
350 	}
351 
352 	CDEBUG(D_INFO, "ll_close exiting\n");
353 	complete(&lcq->lcq_comp);
354 	return 0;
355 }
356 
ll_close_thread_start(struct ll_close_queue ** lcq_ret)357 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
358 {
359 	struct ll_close_queue *lcq;
360 	struct task_struct *task;
361 
362 	if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
363 		return -EINTR;
364 
365 	lcq = kzalloc(sizeof(*lcq), GFP_NOFS);
366 	if (!lcq)
367 		return -ENOMEM;
368 
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);
373 
374 	task = kthread_run(ll_close_thread, lcq, "ll_close");
375 	if (IS_ERR(task)) {
376 		kfree(lcq);
377 		return PTR_ERR(task);
378 	}
379 
380 	wait_for_completion(&lcq->lcq_comp);
381 	*lcq_ret = lcq;
382 	return 0;
383 }
384 
ll_close_thread_shutdown(struct ll_close_queue * lcq)385 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
386 {
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 	kfree(lcq);
392 }
393