1/*
2 * quota.c - NTFS kernel quota ($Quota) handling.  Part of the Linux-NTFS
3 *	     project.
4 *
5 * Copyright (c) 2004 Anton Altaparmakov
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23#ifdef NTFS_RW
24
25#include "index.h"
26#include "quota.h"
27#include "debug.h"
28#include "ntfs.h"
29
30/**
31 * ntfs_mark_quotas_out_of_date - mark the quotas out of date on an ntfs volume
32 * @vol:	ntfs volume on which to mark the quotas out of date
33 *
34 * Mark the quotas out of date on the ntfs volume @vol and return 'true' on
35 * success and 'false' on error.
36 */
37bool ntfs_mark_quotas_out_of_date(ntfs_volume *vol)
38{
39	ntfs_index_context *ictx;
40	QUOTA_CONTROL_ENTRY *qce;
41	const le32 qid = QUOTA_DEFAULTS_ID;
42	int err;
43
44	ntfs_debug("Entering.");
45	if (NVolQuotaOutOfDate(vol))
46		goto done;
47	if (!vol->quota_ino || !vol->quota_q_ino) {
48		ntfs_error(vol->sb, "Quota inodes are not open.");
49		return false;
50	}
51	mutex_lock(&vol->quota_q_ino->i_mutex);
52	ictx = ntfs_index_ctx_get(NTFS_I(vol->quota_q_ino));
53	if (!ictx) {
54		ntfs_error(vol->sb, "Failed to get index context.");
55		goto err_out;
56	}
57	err = ntfs_index_lookup(&qid, sizeof(qid), ictx);
58	if (err) {
59		if (err == -ENOENT)
60			ntfs_error(vol->sb, "Quota defaults entry is not "
61					"present.");
62		else
63			ntfs_error(vol->sb, "Lookup of quota defaults entry "
64					"failed.");
65		goto err_out;
66	}
67	if (ictx->data_len < offsetof(QUOTA_CONTROL_ENTRY, sid)) {
68		ntfs_error(vol->sb, "Quota defaults entry size is invalid.  "
69				"Run chkdsk.");
70		goto err_out;
71	}
72	qce = (QUOTA_CONTROL_ENTRY*)ictx->data;
73	if (le32_to_cpu(qce->version) != QUOTA_VERSION) {
74		ntfs_error(vol->sb, "Quota defaults entry version 0x%x is not "
75				"supported.", le32_to_cpu(qce->version));
76		goto err_out;
77	}
78	ntfs_debug("Quota defaults flags = 0x%x.", le32_to_cpu(qce->flags));
79	/* If quotas are already marked out of date, no need to do anything. */
80	if (qce->flags & QUOTA_FLAG_OUT_OF_DATE)
81		goto set_done;
82	/*
83	 * If quota tracking is neither requested, nor enabled and there are no
84	 * pending deletes, no need to mark the quotas out of date.
85	 */
86	if (!(qce->flags & (QUOTA_FLAG_TRACKING_ENABLED |
87			QUOTA_FLAG_TRACKING_REQUESTED |
88			QUOTA_FLAG_PENDING_DELETES)))
89		goto set_done;
90	/*
91	 * Set the QUOTA_FLAG_OUT_OF_DATE bit thus marking quotas out of date.
92	 * This is verified on WinXP to be sufficient to cause windows to
93	 * rescan the volume on boot and update all quota entries.
94	 */
95	qce->flags |= QUOTA_FLAG_OUT_OF_DATE;
96	/* Ensure the modified flags are written to disk. */
97	ntfs_index_entry_flush_dcache_page(ictx);
98	ntfs_index_entry_mark_dirty(ictx);
99set_done:
100	ntfs_index_ctx_put(ictx);
101	mutex_unlock(&vol->quota_q_ino->i_mutex);
102	/*
103	 * We set the flag so we do not try to mark the quotas out of date
104	 * again on remount.
105	 */
106	NVolSetQuotaOutOfDate(vol);
107done:
108	ntfs_debug("Done.");
109	return true;
110err_out:
111	if (ictx)
112		ntfs_index_ctx_put(ictx);
113	mutex_unlock(&vol->quota_q_ino->i_mutex);
114	return false;
115}
116
117#endif /* NTFS_RW */
118