blob: 8644631bf2ba82acc815cf6c814efb79e0b13268 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright (c) 2011, 2012, Intel Corporation.
27 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * lustre/llite/llite_close.c
33 *
34 * Lustre Lite routines to issue a secondary close after writeback
35 */
36
37#include <linux/module.h>
38
39#define DEBUG_SUBSYSTEM S_LLITE
40
Peng Taod7e09d02013-05-02 16:46:55 +080041#include "llite_internal.h"
42
43/** records that a write is in flight */
John L. Hammond3a52f802016-03-30 19:48:48 -040044void vvp_write_pending(struct vvp_object *club, struct vvp_page *page)
Peng Taod7e09d02013-05-02 16:46:55 +080045{
John L. Hammond8c7b0e12016-03-30 19:48:47 -040046 struct ll_inode_info *lli = ll_i2info(club->vob_inode);
Peng Taod7e09d02013-05-02 16:46:55 +080047
Peng Taod7e09d02013-05-02 16:46:55 +080048 spin_lock(&lli->lli_lock);
49 lli->lli_flags |= LLIF_SOM_DIRTY;
John L. Hammond3a52f802016-03-30 19:48:48 -040050 if (page && list_empty(&page->vpg_pending_linkage))
51 list_add(&page->vpg_pending_linkage, &club->vob_pending_list);
Peng Taod7e09d02013-05-02 16:46:55 +080052 spin_unlock(&lli->lli_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080053}
54
55/** records that a write has completed */
John L. Hammond3a52f802016-03-30 19:48:48 -040056void vvp_write_complete(struct vvp_object *club, struct vvp_page *page)
Peng Taod7e09d02013-05-02 16:46:55 +080057{
John L. Hammond8c7b0e12016-03-30 19:48:47 -040058 struct ll_inode_info *lli = ll_i2info(club->vob_inode);
Peng Taod7e09d02013-05-02 16:46:55 +080059 int rc = 0;
60
Peng Taod7e09d02013-05-02 16:46:55 +080061 spin_lock(&lli->lli_lock);
John L. Hammond3a52f802016-03-30 19:48:48 -040062 if (page && !list_empty(&page->vpg_pending_linkage)) {
63 list_del_init(&page->vpg_pending_linkage);
Peng Taod7e09d02013-05-02 16:46:55 +080064 rc = 1;
65 }
66 spin_unlock(&lli->lli_lock);
67 if (rc)
John L. Hammond8c7b0e12016-03-30 19:48:47 -040068 ll_queue_done_writing(club->vob_inode, 0);
Peng Taod7e09d02013-05-02 16:46:55 +080069}
70
71/** Queues DONE_WRITING if
72 * - done writing is allowed;
Oleg Drokinc0894c62016-02-24 22:00:30 -050073 * - inode has no no dirty pages;
74 */
Peng Taod7e09d02013-05-02 16:46:55 +080075void ll_queue_done_writing(struct inode *inode, unsigned long flags)
76{
77 struct ll_inode_info *lli = ll_i2info(inode);
John L. Hammond8c7b0e12016-03-30 19:48:47 -040078 struct vvp_object *club = cl2vvp(ll_i2info(inode)->lli_clob);
Peng Taod7e09d02013-05-02 16:46:55 +080079
80 spin_lock(&lli->lli_lock);
81 lli->lli_flags |= flags;
82
83 if ((lli->lli_flags & LLIF_DONE_WRITING) &&
John L. Hammond8c7b0e12016-03-30 19:48:47 -040084 list_empty(&club->vob_pending_list)) {
Peng Taod7e09d02013-05-02 16:46:55 +080085 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
86
87 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
James Nunez97a075c2016-04-27 18:21:01 -040088 CWARN("%s: file "DFID"(flags %u) Size-on-MDS valid, done writing allowed and no diry pages\n",
89 ll_get_fsname(inode->i_sb, NULL, 0),
90 PFID(ll_inode2fid(inode)), lli->lli_flags);
Peng Taod7e09d02013-05-02 16:46:55 +080091 /* DONE_WRITING is allowed and inode has no dirty page. */
92 spin_lock(&lcq->lcq_lock);
93
94 LASSERT(list_empty(&lli->lli_close_list));
James Nunez97a075c2016-04-27 18:21:01 -040095 CDEBUG(D_INODE, "adding inode "DFID" to close list\n",
96 PFID(ll_inode2fid(inode)));
Peng Taod7e09d02013-05-02 16:46:55 +080097 list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
98
99 /* Avoid a concurrent insertion into the close thread queue:
100 * an inode is already in the close thread, open(), write(),
101 * close() happen, epoch is closed as the inode is marked as
102 * LLIF_EPOCH_PENDING. When pages are written inode should not
103 * be inserted into the queue again, clear this flag to avoid
Oleg Drokinc0894c62016-02-24 22:00:30 -0500104 * it.
105 */
Peng Taod7e09d02013-05-02 16:46:55 +0800106 lli->lli_flags &= ~LLIF_DONE_WRITING;
107
108 wake_up(&lcq->lcq_waitq);
109 spin_unlock(&lcq->lcq_lock);
110 }
111 spin_unlock(&lli->lli_lock);
Peng Taod7e09d02013-05-02 16:46:55 +0800112}
113
114/** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
115void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
116{
117 struct ll_inode_info *lli = ll_i2info(inode);
Peng Taod7e09d02013-05-02 16:46:55 +0800118
119 op_data->op_flags |= MF_SOM_CHANGE;
120 /* Check if Size-on-MDS attributes are valid. */
121 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
James Nunez97a075c2016-04-27 18:21:01 -0400122 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on Size-on-MDS attributes\n",
123 ll_get_fsname(inode->i_sb, NULL, 0),
124 PFID(ll_inode2fid(inode)), lli->lli_flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800125
126 if (!cl_local_size(inode)) {
127 /* Send Size-on-MDS Attributes if valid. */
128 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
129 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
130 }
Peng Taod7e09d02013-05-02 16:46:55 +0800131}
132
133/** Closes ioepoch and packs Size-on-MDS attribute if needed into @op_data. */
134void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
135 struct obd_client_handle **och, unsigned long flags)
136{
137 struct ll_inode_info *lli = ll_i2info(inode);
John L. Hammond8c7b0e12016-03-30 19:48:47 -0400138 struct vvp_object *club = cl2vvp(ll_i2info(inode)->lli_clob);
Peng Taod7e09d02013-05-02 16:46:55 +0800139
140 spin_lock(&lli->lli_lock);
John L. Hammond8c7b0e12016-03-30 19:48:47 -0400141 if (!(list_empty(&club->vob_pending_list))) {
Peng Taod7e09d02013-05-02 16:46:55 +0800142 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
Oleg Drokin6e168182016-02-16 00:46:46 -0500143 LASSERT(*och);
144 LASSERT(!lli->lli_pending_och);
Peng Taod7e09d02013-05-02 16:46:55 +0800145 /* Inode is dirty and there is no pending write done
Oleg Drokinc0894c62016-02-24 22:00:30 -0500146 * request yet, DONE_WRITE is to be sent later.
147 */
Peng Taod7e09d02013-05-02 16:46:55 +0800148 lli->lli_flags |= LLIF_EPOCH_PENDING;
149 lli->lli_pending_och = *och;
150 spin_unlock(&lli->lli_lock);
151
152 inode = igrab(inode);
153 LASSERT(inode);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200154 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800155 }
156 if (flags & LLIF_DONE_WRITING) {
157 /* Some pages are still dirty, it is early to send
Masanari Iidabef31c72013-09-15 14:38:18 +0900158 * DONE_WRITE. Wait until all pages will be flushed
Oleg Drokinc0894c62016-02-24 22:00:30 -0500159 * and try DONE_WRITE again later.
160 */
Peng Taod7e09d02013-05-02 16:46:55 +0800161 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
162 lli->lli_flags |= LLIF_DONE_WRITING;
163 spin_unlock(&lli->lli_lock);
164
165 inode = igrab(inode);
166 LASSERT(inode);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200167 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800168 }
169 }
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700170 CDEBUG(D_INODE, "Epoch %llu closed on "DFID"\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800171 ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
172 op_data->op_flags |= MF_EPOCH_CLOSE;
173
174 if (flags & LLIF_DONE_WRITING) {
175 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
176 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
177 *och = lli->lli_pending_och;
178 lli->lli_pending_och = NULL;
179 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
180 } else {
181 /* Pack Size-on-MDS inode attributes only if they has changed */
182 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
183 spin_unlock(&lli->lli_lock);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200184 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800185 }
186
187 /* There is a pending DONE_WRITE -- close epoch with no
Oleg Drokinc0894c62016-02-24 22:00:30 -0500188 * attribute change.
189 */
Peng Taod7e09d02013-05-02 16:46:55 +0800190 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
191 spin_unlock(&lli->lli_lock);
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200192 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800193 }
194 }
195
John L. Hammond8c7b0e12016-03-30 19:48:47 -0400196 LASSERT(list_empty(&club->vob_pending_list));
Peng Taod7e09d02013-05-02 16:46:55 +0800197 lli->lli_flags &= ~LLIF_SOM_DIRTY;
198 spin_unlock(&lli->lli_lock);
199 ll_done_writing_attr(inode, op_data);
200
Peng Taod7e09d02013-05-02 16:46:55 +0800201out:
202 return;
203}
204
205/**
206 * Cliens updates SOM attributes on MDS (including llog cookies):
207 * obd_getattr with no lock and md_setattr.
208 */
209int ll_som_update(struct inode *inode, struct md_op_data *op_data)
210{
211 struct ll_inode_info *lli = ll_i2info(inode);
212 struct ptlrpc_request *request = NULL;
213 __u32 old_flags;
214 struct obdo *oa;
215 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800216
Oleg Drokin6e168182016-02-16 00:46:46 -0500217 LASSERT(op_data);
Peng Taod7e09d02013-05-02 16:46:55 +0800218 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
James Nunez97a075c2016-04-27 18:21:01 -0400219 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on Size-on-MDS attributes\n",
220 ll_get_fsname(inode->i_sb, NULL, 0),
221 PFID(ll_inode2fid(inode)), lli->lli_flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800222
Amitoj Kaur Chawla21068c42016-02-26 14:25:09 +0530223 oa = kmem_cache_zalloc(obdo_cachep, GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +0800224 if (!oa) {
225 CERROR("can't allocate memory for Size-on-MDS update.\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800226 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800227 }
228
229 old_flags = op_data->op_flags;
230 op_data->op_flags = MF_SOM_CHANGE;
231
232 /* If inode is already in another epoch, skip getattr from OSTs. */
233 if (lli->lli_ioepoch == op_data->op_ioepoch) {
234 rc = ll_inode_getattr(inode, oa, op_data->op_ioepoch,
235 old_flags & MF_GETATTR_LOCK);
236 if (rc) {
237 oa->o_valid = 0;
238 if (rc != -ENOENT)
James Nunez97a075c2016-04-27 18:21:01 -0400239 CERROR("%s: inode_getattr failed - unable to send a Size-on-MDS attribute update for inode "DFID": rc = %d\n",
240 ll_get_fsname(inode->i_sb, NULL, 0),
241 PFID(ll_inode2fid(inode)), rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800242 } else {
243 CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
244 PFID(&lli->lli_fid));
245 }
246 /* Install attributes into op_data. */
247 md_from_obdo(op_data, oa, oa->o_valid);
248 }
249
250 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data,
251 NULL, 0, NULL, 0, &request, NULL);
252 ptlrpc_req_finished(request);
253
Mike Rapoport2ba262f2015-10-20 12:39:46 +0300254 kmem_cache_free(obdo_cachep, oa);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800255 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800256}
257
258/**
259 * Closes the ioepoch and packs all the attributes into @op_data for
260 * DONE_WRITING rpc.
261 */
262static void ll_prepare_done_writing(struct inode *inode,
263 struct md_op_data *op_data,
264 struct obd_client_handle **och)
265{
266 ll_ioepoch_close(inode, op_data, och, LLIF_DONE_WRITING);
267 /* If there is no @och, we do not do D_W yet. */
Oleg Drokin6e168182016-02-16 00:46:46 -0500268 if (!*och)
Peng Taod7e09d02013-05-02 16:46:55 +0800269 return;
270
271 ll_pack_inode2opdata(inode, op_data, &(*och)->och_fh);
272 ll_prep_md_op_data(op_data, inode, NULL, NULL,
273 0, 0, LUSTRE_OPC_ANY, NULL);
274}
275
276/** Send a DONE_WRITING rpc. */
277static void ll_done_writing(struct inode *inode)
278{
279 struct obd_client_handle *och = NULL;
280 struct md_op_data *op_data;
281 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800282
283 LASSERT(exp_connect_som(ll_i2mdexp(inode)));
284
Julia Lawall496a51b2014-09-18 22:24:02 +0200285 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
Quentin Lambert695a0662015-02-12 15:56:07 +0100286 if (!op_data)
Peng Taod7e09d02013-05-02 16:46:55 +0800287 return;
Peng Taod7e09d02013-05-02 16:46:55 +0800288
289 ll_prepare_done_writing(inode, op_data, &och);
290 /* If there is no @och, we do not do D_W yet. */
Oleg Drokin6e168182016-02-16 00:46:46 -0500291 if (!och)
Julia Lawall34e1f2b2014-08-30 16:24:55 +0200292 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800293
294 rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
Shraddha Barked9ec18cf2015-09-05 18:58:23 +0530295 if (rc == -EAGAIN)
Peng Taod7e09d02013-05-02 16:46:55 +0800296 /* MDS has instructed us to obtain Size-on-MDS attribute from
Oleg Drokinc0894c62016-02-24 22:00:30 -0500297 * OSTs and send setattr to back to MDS.
298 */
Peng Taod7e09d02013-05-02 16:46:55 +0800299 rc = ll_som_update(inode, op_data);
James Nunez97a075c2016-04-27 18:21:01 -0400300 else if (rc) {
301 CERROR("%s: inode "DFID" mdc done_writing failed: rc = %d\n",
302 ll_get_fsname(inode->i_sb, NULL, 0),
303 PFID(ll_inode2fid(inode)), rc);
304 }
Peng Taod7e09d02013-05-02 16:46:55 +0800305out:
306 ll_finish_md_op_data(op_data);
307 if (och) {
308 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
Julia Lawall97903a22015-04-12 22:55:02 +0200309 kfree(och);
Peng Taod7e09d02013-05-02 16:46:55 +0800310 }
Peng Taod7e09d02013-05-02 16:46:55 +0800311}
312
313static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
314{
315 struct ll_inode_info *lli = NULL;
316
317 spin_lock(&lcq->lcq_lock);
318
319 if (!list_empty(&lcq->lcq_head)) {
320 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
Oleg Drokine15ba452016-02-26 01:49:49 -0500321 lli_close_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800322 list_del_init(&lli->lli_close_list);
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400323 } else if (atomic_read(&lcq->lcq_stop)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800324 lli = ERR_PTR(-EALREADY);
Oleg Drokinda5ecb42016-04-01 15:18:01 -0400325 }
Peng Taod7e09d02013-05-02 16:46:55 +0800326
327 spin_unlock(&lcq->lcq_lock);
328 return lli;
329}
330
331static int ll_close_thread(void *arg)
332{
333 struct ll_close_queue *lcq = arg;
Peng Taod7e09d02013-05-02 16:46:55 +0800334
335 complete(&lcq->lcq_comp);
336
337 while (1) {
338 struct l_wait_info lwi = { 0 };
339 struct ll_inode_info *lli;
340 struct inode *inode;
341
342 l_wait_event_exclusive(lcq->lcq_waitq,
343 (lli = ll_close_next_lli(lcq)) != NULL,
344 &lwi);
345 if (IS_ERR(lli))
346 break;
347
348 inode = ll_info2i(lli);
James Nunez97a075c2016-04-27 18:21:01 -0400349 CDEBUG(D_INFO, "done_writing for inode "DFID"\n",
350 PFID(ll_inode2fid(inode)));
Peng Taod7e09d02013-05-02 16:46:55 +0800351 ll_done_writing(inode);
352 iput(inode);
353 }
354
355 CDEBUG(D_INFO, "ll_close exiting\n");
356 complete(&lcq->lcq_comp);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800357 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800358}
359
360int ll_close_thread_start(struct ll_close_queue **lcq_ret)
361{
362 struct ll_close_queue *lcq;
Greg Kroah-Hartman68b636b2013-08-04 08:56:42 +0800363 struct task_struct *task;
Peng Taod7e09d02013-05-02 16:46:55 +0800364
365 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
366 return -EINTR;
367
Julia Lawall496a51b2014-09-18 22:24:02 +0200368 lcq = kzalloc(sizeof(*lcq), GFP_NOFS);
369 if (!lcq)
Peng Taod7e09d02013-05-02 16:46:55 +0800370 return -ENOMEM;
371
372 spin_lock_init(&lcq->lcq_lock);
373 INIT_LIST_HEAD(&lcq->lcq_head);
374 init_waitqueue_head(&lcq->lcq_waitq);
375 init_completion(&lcq->lcq_comp);
376
377 task = kthread_run(ll_close_thread, lcq, "ll_close");
378 if (IS_ERR(task)) {
Julia Lawall97903a22015-04-12 22:55:02 +0200379 kfree(lcq);
Peng Taod7e09d02013-05-02 16:46:55 +0800380 return PTR_ERR(task);
381 }
382
383 wait_for_completion(&lcq->lcq_comp);
384 *lcq_ret = lcq;
385 return 0;
386}
387
388void ll_close_thread_shutdown(struct ll_close_queue *lcq)
389{
390 init_completion(&lcq->lcq_comp);
391 atomic_inc(&lcq->lcq_stop);
392 wake_up(&lcq->lcq_waitq);
393 wait_for_completion(&lcq->lcq_comp);
Julia Lawall97903a22015-04-12 22:55:02 +0200394 kfree(lcq);
Peng Taod7e09d02013-05-02 16:46:55 +0800395}