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