summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/quotactl.cc
blob: 7218b54adba167c16046b82d46710faf34a4cef6 (plain)
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
/* quotactl.cc: code for manipulating disk quotas

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "cygtls.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "ntdll.h"
#include "tls_pbuf.h"
#include <sys/mount.h>
#include <sys/quota.h>

#define PGQI_SIZE (sizeof (FILE_GET_QUOTA_INFORMATION) + SECURITY_MAX_SID_SIZE)
#define PFQI_SIZE (sizeof (FILE_QUOTA_INFORMATION) + SECURITY_MAX_SID_SIZE)

/* Modelled after the Linux quotactl function. */
extern "C" int
quotactl (int cmd, const char *special, int id, caddr_t addr)
{
  ACCESS_MASK access = FILE_READ_DATA;
  PSID sid = NO_SID;
  path_conv pc;
  tmp_pathbuf tp;
  UNICODE_STRING path;
  OBJECT_ATTRIBUTES attr;
  NTSTATUS status;
  HANDLE fh;
  IO_STATUS_BLOCK io;
  FILE_FS_CONTROL_INFORMATION ffci;
  int ret = 0;

  uint32_t subcmd = (uint32_t) cmd >> SUBCMDSHIFT;
  uint32_t type = (uint32_t) cmd & SUBCMDMASK;

  if (type != USRQUOTA && type != GRPQUOTA)
    {
      set_errno (EINVAL);
      return -1;
    }
  switch (subcmd)
    {
    case Q_SYNC:
      if (!special)
	return 0;
      access |= FILE_WRITE_DATA;
      break;
    case Q_QUOTAON:
      if (id < QFMT_VFS_OLD || id > QFMT_VFS_V1)
	{
	  set_errno (EINVAL);
	  return -1;
	}
      /*FALLTHRU*/
    case Q_QUOTAOFF:
    case Q_SETINFO:
      access |= FILE_WRITE_DATA;
      break;
    case Q_GETFMT:
    case Q_GETINFO:
      break;
    case Q_SETQUOTA:
      access |= FILE_WRITE_DATA;
      /*FALLTHRU*/
    case Q_GETQUOTA:
      /* Windows feature: Default limits.  Get or set them with id == -1. */
      if (id != -1)
	{
	  if (type == USRQUOTA)
	    sid = sidfromuid (id, NULL);
	  else
	    sid = sidfromgid (id, NULL);
	  if (sid == NO_SID)
	    {
	      set_errno (EINVAL);
	      return -1;
	    }
	}
      break;
    default:
      set_errno (EINVAL);
      return -1;
    }
  /* Check path */
  pc.check (special, PC_SYM_FOLLOW | PC_NOWARN, stat_suffixes);
  if (pc.error)
    {
      set_errno (pc.error);
      return -1;
    }
  if (!pc.exists ())
    {
      set_errno (ENOENT);
      return -1;
    }
  if (!S_ISBLK (pc.dev.mode))
    {
      set_errno (ENOTBLK);
      return -1;
    }
  pc.get_object_attr (attr, sec_none_nih);
  /* For the following functions to work, we must attach the virtual path to 
     the quota file to the device path.
     
     FIXME: Note that this is NTFS-specific.  Adding ReFS in another step. */
  tp.u_get (&path);
  RtlCopyUnicodeString (&path, attr.ObjectName);
  RtlAppendUnicodeToString (&path, L"\\$Extend\\$Quota:$Q:$INDEX_ALLOCATION");
  attr.ObjectName = &path;

  /* Open filesystem */
  status = NtOpenFile (&fh, access, &attr, &io, FILE_SHARE_VALID_FLAGS, 0);
  if (NT_SUCCESS (status))
    switch (subcmd)
      {
      case Q_SYNC:
	/* No sync, just report success. */
	status = STATUS_SUCCESS;
      	break;
      case Q_QUOTAON:
      case Q_QUOTAOFF:
	/* Ignore filename in addr. */
	status = NtQueryVolumeInformationFile (fh, &io, &ffci, sizeof ffci,
					       FileFsControlInformation);
	if (!NT_SUCCESS (status))
	  break;
	ffci.FileSystemControlFlags &= ~FILE_VC_QUOTA_ENFORCE
				       & ~FILE_VC_QUOTA_TRACK
				       & ~FILE_VC_QUOTAS_INCOMPLETE
				       & ~FILE_VC_QUOTAS_REBUILDING;
	if (subcmd == Q_QUOTAON)
	  ffci.FileSystemControlFlags |= FILE_VC_QUOTA_ENFORCE;
	status = NtSetVolumeInformationFile (fh, &io, &ffci, sizeof ffci,
					     FileFsControlInformation);
	break;
      case Q_GETFMT:
      	__try
	  {
	    uint32_t *retval = (uint32_t *) addr;

	    /* Always fake the latest format. */
	    *retval = QFMT_VFS_V1;
	  }
	__except (EFAULT)
	  {
	    ret = -1;
	    break;
	  }
	__endtry
	status = STATUS_SUCCESS;
	break;
      case Q_GETINFO:
	__try
	  {
	    struct dqinfo *dqi = (struct dqinfo *) addr;

	    dqi->dqi_bgrace = dqi->dqi_igrace = UINT64_MAX;
	    dqi->dqi_flags = 0;
	    dqi->dqi_valid = IIF_BGRACE | IIF_IGRACE;
	  }
	__except (EFAULT)
	  {
	    ret = -1;
	    break;
	  }
	__endtry
	status = STATUS_SUCCESS;
	break;
      case Q_SETINFO:
	/* No settings possible, just report success. */
	status = STATUS_SUCCESS;
      	break;
      case Q_GETQUOTA:
	/* Windows feature: Default limits.  Get or set them with id == -1. */
	if (id == -1)
	  {
	    status = NtQueryVolumeInformationFile (fh, &io, &ffci, sizeof ffci,
						   FileFsControlInformation);
	    if (!NT_SUCCESS (status))
	      break;
	    __try
	      {
		struct dqblk *dq = (struct dqblk *) addr;

		dq->dqb_bhardlimit = (uint64_t) ffci.DefaultQuotaLimit.QuadPart;
		if (dq->dqb_bhardlimit != UINT64_MAX)
		  dq->dqb_bhardlimit /= BLOCK_SIZE;
		dq->dqb_bsoftlimit =
				(uint64_t) ffci.DefaultQuotaThreshold.QuadPart;
		if (dq->dqb_bsoftlimit != UINT64_MAX)
		  dq->dqb_bsoftlimit /= BLOCK_SIZE;
		dq->dqb_curspace = 0;
		dq->dqb_ihardlimit = UINT64_MAX;
		dq->dqb_isoftlimit = UINT64_MAX;
		dq->dqb_curinodes = 0;
		dq->dqb_btime = UINT64_MAX;
		dq->dqb_itime = UINT64_MAX;
		dq->dqb_valid = QIF_BLIMITS;
	      }
	    __except (EFAULT)
	      {
		ret = -1;
		break;
	      }
	    __endtry
	  }
	else
	  {
	    PFILE_GET_QUOTA_INFORMATION pgqi = (PFILE_GET_QUOTA_INFORMATION)
					       alloca (PGQI_SIZE);
	    PFILE_QUOTA_INFORMATION pfqi = (PFILE_QUOTA_INFORMATION)
					   alloca (PFQI_SIZE);

	    pgqi->NextEntryOffset = 0;
	    pgqi->SidLength = RtlLengthSid (sid);
	    RtlCopySid (RtlLengthSid (sid), &pgqi->Sid, sid);
	    status = NtQueryQuotaInformationFile (fh, &io, pfqi, PFQI_SIZE,
						  TRUE, pgqi, PGQI_SIZE,
						  NULL, TRUE);
	    if (!NT_SUCCESS (status))
	      break;
	    __try
	      {
		struct dqblk *dq = (struct dqblk *) addr;

		dq->dqb_bhardlimit = (uint64_t) pfqi->QuotaLimit.QuadPart;
		if (dq->dqb_bhardlimit != UINT64_MAX)
		  dq->dqb_bhardlimit /= BLOCK_SIZE;
		dq->dqb_bsoftlimit = (uint64_t) pfqi->QuotaThreshold.QuadPart;
		if (dq->dqb_bsoftlimit != UINT64_MAX)
		  dq->dqb_bsoftlimit /= BLOCK_SIZE;
		dq->dqb_curspace = (uint64_t) pfqi->QuotaUsed.QuadPart;
		if (dq->dqb_curspace != UINT64_MAX)
		  dq->dqb_curspace /= BLOCK_SIZE;
		dq->dqb_ihardlimit = UINT64_MAX;
		dq->dqb_isoftlimit = UINT64_MAX;
		dq->dqb_curinodes = 0;
		dq->dqb_btime = UINT64_MAX;
		dq->dqb_itime = UINT64_MAX;
		dq->dqb_valid = QIF_BLIMITS | QIF_SPACE;
	      }
	    __except (EFAULT)
	      {
		ret = -1;
		break;
	      }
	    __endtry
	  }
	break;
      case Q_SETQUOTA:
	/* Windows feature: Default limits.  Get or set them with id == -1. */
	if (id == -1)
	  {
	    status = NtQueryVolumeInformationFile (fh, &io, &ffci, sizeof ffci,
						   FileFsControlInformation);
	    if (!NT_SUCCESS (status))
	      break;
	    __try
	      {
		struct dqblk *dq = (struct dqblk *) addr;

		if (!(dq->dqb_valid & QIF_BLIMITS))
		  break;
		ffci.DefaultQuotaLimit.QuadPart = dq->dqb_bhardlimit;
		if (ffci.DefaultQuotaLimit.QuadPart != -1)
		  ffci.DefaultQuotaLimit.QuadPart *= BLOCK_SIZE;
		ffci.DefaultQuotaThreshold.QuadPart = dq->dqb_bsoftlimit;
		if (ffci.DefaultQuotaThreshold.QuadPart != -1)
		  ffci.DefaultQuotaThreshold.QuadPart *= BLOCK_SIZE;
	      }
	    __except (EFAULT)
	      {
	        ret = -1;
		break;
	      }
	    __endtry
	    status = NtSetVolumeInformationFile (fh, &io, &ffci, sizeof ffci,
						 FileFsControlInformation);
	  }
	else
	  {
	    PFILE_GET_QUOTA_INFORMATION pgqi = (PFILE_GET_QUOTA_INFORMATION)
					       alloca (PGQI_SIZE);
	    PFILE_QUOTA_INFORMATION pfqi = (PFILE_QUOTA_INFORMATION)
					   alloca (PFQI_SIZE);

	    pgqi->NextEntryOffset = 0;
	    pgqi->SidLength = RtlLengthSid (sid);
	    RtlCopySid (RtlLengthSid (sid), &pgqi->Sid, sid);
	    status = NtQueryQuotaInformationFile (fh, &io, pfqi, PFQI_SIZE,
						  TRUE, pgqi, PGQI_SIZE,
						  NULL, TRUE);
	    if (!NT_SUCCESS (status))
	      break;
	    __try
	      {
		struct dqblk *dq = (struct dqblk *) addr;

		if (!(dq->dqb_valid & QIF_BLIMITS))
		  break;
		pfqi->QuotaLimit.QuadPart = dq->dqb_bhardlimit;
		if (pfqi->QuotaLimit.QuadPart != -1)
		  pfqi->QuotaLimit.QuadPart *= BLOCK_SIZE;
		pfqi->QuotaThreshold.QuadPart = dq->dqb_bsoftlimit;
		if (pfqi->QuotaThreshold.QuadPart != -1)
		  pfqi->QuotaThreshold.QuadPart *= BLOCK_SIZE;
	      }
	    __except (EFAULT)
	      {
		ret = -1;
		break;
	      }
	    __endtry
	    status = NtSetQuotaInformationFile (fh, &io, pfqi, PFQI_SIZE);
	  }
	break;
      }
  if (!NT_SUCCESS (status))
    {
      __seterrno_from_nt_status (status);
      ret = -1;
    }
  return ret;
}