summaryrefslogtreecommitdiffstats
path: root/source4/ntvfs/posix/pvfs_setfileinfo.c
blob: 7fd4e35d4f5606b490e6e1c1ed69bac4eb3a49d2 (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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/* 
   Unix SMB/CIFS implementation.

   POSIX NTVFS backend - setfileinfo

   Copyright (C) Andrew Tridgell 2004

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   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 for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "vfs_posix.h"
#include "system/time.h"
#include "librpc/gen_ndr/xattr.h"


/*
  determine what access bits are needed for a call
*/
static uint32_t pvfs_setfileinfo_access(union smb_setfileinfo *info)
{
	uint32_t needed;

	switch (info->generic.level) {
	case RAW_SFILEINFO_EA_SET:
		needed = SEC_FILE_WRITE_EA;
		break;

	case RAW_SFILEINFO_DISPOSITION_INFO:
	case RAW_SFILEINFO_DISPOSITION_INFORMATION:
		needed = SEC_STD_DELETE;
		break;

	case RAW_SFILEINFO_END_OF_FILE_INFO:
		needed = SEC_FILE_WRITE_DATA;
		break;

	case RAW_SFILEINFO_POSITION_INFORMATION:
		needed = 0;
		break;

	case RAW_SFILEINFO_SEC_DESC:
		needed = 0;
		if (info->set_secdesc.in.secinfo_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
			needed |= SEC_STD_WRITE_OWNER;
		}
		if (info->set_secdesc.in.secinfo_flags & SECINFO_DACL) {
			needed |= SEC_STD_WRITE_DAC;
		}
		if (info->set_secdesc.in.secinfo_flags & SECINFO_SACL) {
			needed |= SEC_FLAG_SYSTEM_SECURITY;
		}
		break;

	case RAW_SFILEINFO_RENAME_INFORMATION:
	case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
		needed = SEC_STD_DELETE;
		break;

	default:
		needed = SEC_FILE_WRITE_ATTRIBUTE;
		break;
	}

	return needed;
}

/*
  rename_information level for streams
*/
static NTSTATUS pvfs_setfileinfo_rename_stream(struct pvfs_state *pvfs, 
					       struct ntvfs_request *req, 
					       struct pvfs_filename *name,
					       int fd,
					       DATA_BLOB *odb_locking_key,
					       union smb_setfileinfo *info)
{
	NTSTATUS status;
	struct odb_lock *lck = NULL;

	/* strangely, this gives a sharing violation, not invalid
	   parameter */
	if (info->rename_information.in.new_name[0] != ':') {
		return NT_STATUS_SHARING_VIOLATION;
	}

	status = pvfs_access_check_simple(pvfs, req, name, SEC_FILE_WRITE_ATTRIBUTE);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	lck = odb_lock(req, pvfs->odb_context, odb_locking_key);
	if (lck == NULL) {
		DEBUG(0,("Unable to lock opendb for can_stat\n"));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}


	status = pvfs_stream_rename(pvfs, name, fd, 
				    info->rename_information.in.new_name+1,
				    info->rename_information.in.overwrite);
	return status;
}

/*
  rename_information level
*/
static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs, 
					struct ntvfs_request *req, 
					struct pvfs_filename *name,
					int fd,
					DATA_BLOB *odb_locking_key,
					union smb_setfileinfo *info)
{
	NTSTATUS status;
	struct pvfs_filename *name2;
	char *new_name, *p;
	struct odb_lock *lck = NULL;

	/* renames are only allowed within a directory */
	if (strchr_m(info->rename_information.in.new_name, '\\') &&
	    (req->ctx->protocol < PROTOCOL_SMB2_02)) {
		return NT_STATUS_NOT_SUPPORTED;
	}

	/* handle stream renames specially */
	if (name->stream_name) {
		return pvfs_setfileinfo_rename_stream(pvfs, req, name, fd, 
						      odb_locking_key, info);
	}

	/* w2k3 does not appear to allow relative rename. On SMB2, vista sends it sometimes,
	   but I suspect it is just uninitialised memory */
	if (info->rename_information.in.root_fid != 0 && 
	    (req->ctx->protocol < PROTOCOL_SMB2_02)) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	/* construct the fully qualified windows name for the new file name */
	if (req->ctx->protocol >= PROTOCOL_SMB2_02) {
		/* SMB2 sends the full path of the new name */
		new_name = talloc_asprintf(req, "\\%s", info->rename_information.in.new_name);
	} else {
		new_name = talloc_strdup(req, name->original_name);
		if (new_name == NULL) {
			return NT_STATUS_NO_MEMORY;
		}
		p = strrchr_m(new_name, '\\');
		if (p == NULL) {
			return NT_STATUS_OBJECT_NAME_INVALID;
		} else {
			*p = 0;
		}

		new_name = talloc_asprintf(req, "%s\\%s", new_name,
					   info->rename_information.in.new_name);
	}
	if (new_name == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/* resolve the new name */
	status = pvfs_resolve_name(pvfs, req, new_name, 0, &name2);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* if the destination exists, then check the rename is allowed */
	if (name2->exists) {
		if (strcmp(name2->full_name, name->full_name) == 0) {
			/* rename to same name is null-op */
			return NT_STATUS_OK;
		}

		if (!info->rename_information.in.overwrite) {
			return NT_STATUS_OBJECT_NAME_COLLISION;
		}

		status = pvfs_can_delete(pvfs, req, name2, NULL);
		if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
			return NT_STATUS_ACCESS_DENIED;
		}
		if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
			return NT_STATUS_ACCESS_DENIED;
		}
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}
	}

	status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	lck = odb_lock(req, pvfs->odb_context, odb_locking_key);
	if (lck == NULL) {
		DEBUG(0,("Unable to lock opendb for can_stat\n"));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = pvfs_do_rename(pvfs, lck, name, name2->full_name);
	talloc_free(lck);
	NT_STATUS_NOT_OK_RETURN(status);
	if (NT_STATUS_IS_OK(status)) {
		name->full_name = talloc_steal(name, name2->full_name);
		name->original_name = talloc_steal(name, name2->original_name);
	}

	return NT_STATUS_OK;
}

/*
  add a single DOS EA
*/
NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs, 
				 struct pvfs_filename *name,
				 int fd, uint16_t num_eas,
				 struct ea_struct *eas)
{
	struct xattr_DosEAs *ealist;
	int i, j;
	NTSTATUS status;

	if (num_eas == 0) {
		return NT_STATUS_OK;
	}

	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_NOT_SUPPORTED;
	}

	ealist = talloc(name, struct xattr_DosEAs);

	/* load the current list */
	status = pvfs_doseas_load(pvfs, name, fd, ealist);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	for (j=0;j<num_eas;j++) {
		struct ea_struct *ea = &eas[j];
		/* see if its already there */
		for (i=0;i<ealist->num_eas;i++) {
			if (strcasecmp_m(ealist->eas[i].name, ea->name.s) == 0) {
				ealist->eas[i].value = ea->value;
				break;
			}
		}

		if (i==ealist->num_eas) {
			/* add it */
			ealist->eas = talloc_realloc(ealist, ealist->eas, 
						       struct xattr_EA, 
						       ealist->num_eas+1);
			if (ealist->eas == NULL) {
				return NT_STATUS_NO_MEMORY;
			}
			ealist->eas[i].name = ea->name.s;
			ealist->eas[i].value = ea->value;
			ealist->num_eas++;
		}
	}
	
	/* pull out any null EAs */
	for (i=0;i<ealist->num_eas;i++) {
		if (ealist->eas[i].value.length == 0) {
			memmove(&ealist->eas[i],
				&ealist->eas[i+1],
				(ealist->num_eas-(i+1)) * sizeof(ealist->eas[i]));
			ealist->num_eas--;
			i--;
		}
	}

	status = pvfs_doseas_save(pvfs, name, fd, ealist);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	notify_trigger(pvfs->notify_context, 
		       NOTIFY_ACTION_MODIFIED, 
		       FILE_NOTIFY_CHANGE_EA,
		       name->full_name);

	name->dos.ea_size = 4;
	for (i=0;i<ealist->num_eas;i++) {
		name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 + 
			ealist->eas[i].value.length;
	}

	/* update the ea_size attrib */
	return pvfs_dosattrib_save(pvfs, name, fd);
}

/*
  set info on a open file
*/
NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
			  struct ntvfs_request *req, 
			  union smb_setfileinfo *info)
{
	struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
				  struct pvfs_state);
	struct pvfs_file *f;
	struct pvfs_file_handle *h;
	struct pvfs_filename newstats;
	NTSTATUS status;
	uint32_t access_needed;
	uint32_t change_mask = 0;

	f = pvfs_find_fd(pvfs, req, info->generic.in.file.ntvfs);
	if (!f) {
		return NT_STATUS_INVALID_HANDLE;
	}

	h = f->handle;

	access_needed = pvfs_setfileinfo_access(info);
	if ((f->access_mask & access_needed) != access_needed) {
		return NT_STATUS_ACCESS_DENIED;
	}

	/* update the file information */
	status = pvfs_resolve_name_handle(pvfs, h);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* we take a copy of the current file stats, then update
	   newstats in each of the elements below. At the end we
	   compare, and make any changes needed */
	newstats = *h->name;

	switch (info->generic.level) {
	case RAW_SFILEINFO_SETATTR:
		if (!null_time(info->setattr.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
		}
		if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
			newstats.dos.attrib = info->setattr.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_SETATTRE:
	case RAW_SFILEINFO_STANDARD:
		if (!null_time(info->setattre.in.create_time)) {
			unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
		}
		if (!null_time(info->setattre.in.access_time)) {
			unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
		}
		if (!null_time(info->setattre.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
		}
  		break;

	case RAW_SFILEINFO_EA_SET:
		return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd,
					       info->ea_set.in.num_eas,
					       info->ea_set.in.eas);

	case RAW_SFILEINFO_BASIC_INFO:
	case RAW_SFILEINFO_BASIC_INFORMATION:
		if (!null_nttime(info->basic_info.in.create_time)) {
			newstats.dos.create_time = info->basic_info.in.create_time;
		}
		if (!null_nttime(info->basic_info.in.access_time)) {
			newstats.dos.access_time = info->basic_info.in.access_time;
		}
		if (!null_nttime(info->basic_info.in.write_time)) {
			newstats.dos.write_time = info->basic_info.in.write_time;
		}
		if (!null_nttime(info->basic_info.in.change_time)) {
			newstats.dos.change_time = info->basic_info.in.change_time;
		}
		if (info->basic_info.in.attrib != 0) {
			newstats.dos.attrib = info->basic_info.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_DISPOSITION_INFO:
	case RAW_SFILEINFO_DISPOSITION_INFORMATION:
		return pvfs_set_delete_on_close(pvfs, req, f, 
						info->disposition_info.in.delete_on_close);

	case RAW_SFILEINFO_ALLOCATION_INFO:
	case RAW_SFILEINFO_ALLOCATION_INFORMATION:
		status = pvfs_break_level2_oplocks(f);
		NT_STATUS_NOT_OK_RETURN(status);

		newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
		if (newstats.dos.alloc_size < newstats.st.st_size) {
			newstats.st.st_size = newstats.dos.alloc_size;
		}
		newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
								newstats.dos.alloc_size);
		break;

	case RAW_SFILEINFO_END_OF_FILE_INFO:
	case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
		status = pvfs_break_level2_oplocks(f);
		NT_STATUS_NOT_OK_RETURN(status);

		newstats.st.st_size = info->end_of_file_info.in.size;
		break;

	case RAW_SFILEINFO_POSITION_INFORMATION:
		h->position = info->position_information.in.position;
		break;

	case RAW_SFILEINFO_FULL_EA_INFORMATION:
		return pvfs_setfileinfo_ea_set(pvfs, h->name, h->fd,
				info->full_ea_information.in.eas.num_eas,
				info->full_ea_information.in.eas.eas);

	case RAW_SFILEINFO_MODE_INFORMATION:
		/* this one is a puzzle */
		if (info->mode_information.in.mode != 0 &&
		    info->mode_information.in.mode != 2 &&
		    info->mode_information.in.mode != 4 &&
		    info->mode_information.in.mode != 6) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		h->mode = info->mode_information.in.mode;
		break;

	case RAW_SFILEINFO_RENAME_INFORMATION:
	case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
		return pvfs_setfileinfo_rename(pvfs, req, h->name, f->handle->fd,
					       &h->odb_locking_key,
					       info);

	case RAW_SFILEINFO_SEC_DESC:
		notify_trigger(pvfs->notify_context, 
			       NOTIFY_ACTION_MODIFIED, 
			       FILE_NOTIFY_CHANGE_SECURITY,
			       h->name->full_name);
		return pvfs_acl_set(pvfs, req, h->name, h->fd, f->access_mask, info);

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	/* possibly change the file size */
	if (newstats.st.st_size != h->name->st.st_size) {
		if (h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
			return NT_STATUS_FILE_IS_A_DIRECTORY;
		}
		if (h->name->stream_name) {
			status = pvfs_stream_truncate(pvfs, h->name, h->fd, newstats.st.st_size);
			if (!NT_STATUS_IS_OK(status)) {
				return status;
			}
			
			change_mask |= FILE_NOTIFY_CHANGE_STREAM_SIZE;
		} else {
			int ret;
			if (f->access_mask & 
			    (SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA)) {
				ret = ftruncate(h->fd, newstats.st.st_size);
			} else {
				ret = truncate(h->name->full_name, newstats.st.st_size);
			}
			if (ret == -1) {
				return pvfs_map_errno(pvfs, errno);
			}
			change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
		}
	}

	/* possibly change the file timestamps */
	if (newstats.dos.create_time != h->name->dos.create_time) {
		change_mask |= FILE_NOTIFY_CHANGE_CREATION;
	}
	if (newstats.dos.access_time != h->name->dos.access_time) {
		change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
	}
	if (newstats.dos.write_time != h->name->dos.write_time) {
		change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
	}
	if ((change_mask & FILE_NOTIFY_CHANGE_LAST_ACCESS) ||
	    (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE)) {
		struct timeval tv[2];

		nttime_to_timeval(&tv[0], newstats.dos.access_time);
		nttime_to_timeval(&tv[1], newstats.dos.write_time);

		if (!timeval_is_zero(&tv[0]) || !timeval_is_zero(&tv[1])) {
			if (utimes(h->name->full_name, tv) == -1) {
				DEBUG(0,("pvfs_setfileinfo: utimes() failed '%s' - %s\n",
					 h->name->full_name, strerror(errno)));
				return pvfs_map_errno(pvfs, errno);
			}
		}
	}
	if (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE) {
		struct odb_lock *lck;

		lck = odb_lock(req, h->pvfs->odb_context, &h->odb_locking_key);
		if (lck == NULL) {
			DEBUG(0,("Unable to lock opendb for write time update\n"));
			return NT_STATUS_INTERNAL_ERROR;
		}

		status = odb_set_write_time(lck, newstats.dos.write_time, true);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0,("Unable to update write time: %s\n",
				nt_errstr(status)));
			talloc_free(lck);
			return status;
		}

		talloc_free(lck);

		h->write_time.update_forced = true;
		h->write_time.update_on_close = false;
		talloc_free(h->write_time.update_event);
		h->write_time.update_event = NULL;
	}

	/* possibly change the attribute */
	if (newstats.dos.attrib != h->name->dos.attrib) {
		mode_t mode;
		if ((newstats.dos.attrib & FILE_ATTRIBUTE_DIRECTORY) &&
		    !(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
		if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
			if (pvfs_sys_fchmod(pvfs, h->fd, mode, h->name->allow_override) == -1) {
				return pvfs_map_errno(pvfs, errno);
			}
		}
		change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
	}

	*h->name = newstats;

	notify_trigger(pvfs->notify_context, 
		       NOTIFY_ACTION_MODIFIED, 
		       change_mask,
		       h->name->full_name);

	return pvfs_dosattrib_save(pvfs, h->name, h->fd);
}

/*
  retry an open after a sharing violation
*/
static void pvfs_retry_setpathinfo(struct pvfs_odb_retry *r,
				   struct ntvfs_module_context *ntvfs,
				   struct ntvfs_request *req,
				   void *_info,
				   void *private_data,
				   enum pvfs_wait_notice reason)
{
	union smb_setfileinfo *info = talloc_get_type(_info,
				      union smb_setfileinfo);
	NTSTATUS status = NT_STATUS_INTERNAL_ERROR;

	talloc_free(r);

	switch (reason) {
	case PVFS_WAIT_CANCEL:
/*TODO*/
		status = NT_STATUS_CANCELLED;
		break;
	case PVFS_WAIT_TIMEOUT:
		/* if it timed out, then give the failure
		   immediately */
/*TODO*/
		status = NT_STATUS_SHARING_VIOLATION;
		break;
	case PVFS_WAIT_EVENT:

		/* try the open again, which could trigger another retry setup
		   if it wants to, so we have to unmark the async flag so we
		   will know if it does a second async reply */
		req->async_states->state &= ~NTVFS_ASYNC_STATE_ASYNC;

		status = pvfs_setpathinfo(ntvfs, req, info);
		if (req->async_states->state & NTVFS_ASYNC_STATE_ASYNC) {
			/* the 2nd try also replied async, so we don't send
			   the reply yet */
			return;
		}

		/* re-mark it async, just in case someone up the chain does
		   paranoid checking */
		req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
		break;
	}

	/* send the reply up the chain */
	req->async_states->status = status;
	req->async_states->send_fn(req);
}

/*
  setup for a unlink retry after a sharing violation
  or a non granted oplock
*/
static NTSTATUS pvfs_setpathinfo_setup_retry(struct ntvfs_module_context *ntvfs,
					     struct ntvfs_request *req,
					     union smb_setfileinfo *info,
					     struct odb_lock *lck,
					     NTSTATUS status)
{
	struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
				  struct pvfs_state);
	struct timeval end_time;

	if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
		end_time = timeval_add(&req->statistics.request_time,
				       0, pvfs->sharing_violation_delay);
	} else if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
		end_time = timeval_add(&req->statistics.request_time,
				       pvfs->oplock_break_timeout, 0);
	} else {
		return NT_STATUS_INTERNAL_ERROR;
	}

	return pvfs_odb_retry_setup(ntvfs, req, lck, end_time, info, NULL,
				    pvfs_retry_setpathinfo);
}

/*
  set info on a pathname
*/
NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
			  struct ntvfs_request *req, union smb_setfileinfo *info)
{
	struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
				  struct pvfs_state);
	struct pvfs_filename *name;
	struct pvfs_filename newstats;
	NTSTATUS status;
	uint32_t access_needed;
	uint32_t change_mask = 0;
	struct odb_lock *lck = NULL;
	DATA_BLOB odb_locking_key;

	/* resolve the cifs name to a posix name */
	status = pvfs_resolve_name(pvfs, req, info->generic.in.file.path, 
				   PVFS_RESOLVE_STREAMS, &name);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (!name->exists) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	access_needed = pvfs_setfileinfo_access(info);
	status = pvfs_access_check_simple(pvfs, req, name, access_needed);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* we take a copy of the current file stats, then update
	   newstats in each of the elements below. At the end we
	   compare, and make any changes needed */
	newstats = *name;

	switch (info->generic.level) {
	case RAW_SFILEINFO_SETATTR:
		if (!null_time(info->setattr.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
		}
		if (info->setattr.in.attrib == 0) {
			newstats.dos.attrib = FILE_ATTRIBUTE_NORMAL;
		} else if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
			newstats.dos.attrib = info->setattr.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_SETATTRE:
	case RAW_SFILEINFO_STANDARD:
		if (!null_time(info->setattre.in.create_time)) {
			unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
		}
		if (!null_time(info->setattre.in.access_time)) {
			unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
		}
		if (!null_time(info->setattre.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
		}
  		break;

	case RAW_SFILEINFO_EA_SET:
		return pvfs_setfileinfo_ea_set(pvfs, name, -1, 
					       info->ea_set.in.num_eas,
					       info->ea_set.in.eas);

	case RAW_SFILEINFO_BASIC_INFO:
	case RAW_SFILEINFO_BASIC_INFORMATION:
		if (!null_nttime(info->basic_info.in.create_time)) {
			newstats.dos.create_time = info->basic_info.in.create_time;
		}
		if (!null_nttime(info->basic_info.in.access_time)) {
			newstats.dos.access_time = info->basic_info.in.access_time;
		}
		if (!null_nttime(info->basic_info.in.write_time)) {
			newstats.dos.write_time = info->basic_info.in.write_time;
		}
		if (!null_nttime(info->basic_info.in.change_time)) {
			newstats.dos.change_time = info->basic_info.in.change_time;
		}
		if (info->basic_info.in.attrib != 0) {
			newstats.dos.attrib = info->basic_info.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_ALLOCATION_INFO:
	case RAW_SFILEINFO_ALLOCATION_INFORMATION:
		status = pvfs_can_update_file_size(pvfs, req, name, &lck);
		/*
		 * on a sharing violation we need to retry when the file is closed by
		 * the other user, or after 1 second
		 * on a non granted oplock we need to retry when the file is closed by
		 * the other user, or after 30 seconds
		*/
		if ((NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
		     NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) &&
		    (req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
			return pvfs_setpathinfo_setup_retry(pvfs->ntvfs, req, info, lck, status);
		}
		NT_STATUS_NOT_OK_RETURN(status);

		if (info->allocation_info.in.alloc_size > newstats.dos.alloc_size) {
			/* strange. Increasing the allocation size via setpathinfo 
			   should be silently ignored */
			break;
		}
		newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
		if (newstats.dos.alloc_size < newstats.st.st_size) {
			newstats.st.st_size = newstats.dos.alloc_size;
		}
		newstats.dos.alloc_size = pvfs_round_alloc_size(pvfs, 
								newstats.dos.alloc_size);
		break;

	case RAW_SFILEINFO_END_OF_FILE_INFO:
	case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
		status = pvfs_can_update_file_size(pvfs, req, name, &lck);
		/*
		 * on a sharing violation we need to retry when the file is closed by
		 * the other user, or after 1 second
		 * on a non granted oplock we need to retry when the file is closed by
		 * the other user, or after 30 seconds
		*/
		if ((NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
		     NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) &&
		    (req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
			return pvfs_setpathinfo_setup_retry(pvfs->ntvfs, req, info, lck, status);
		}
		NT_STATUS_NOT_OK_RETURN(status);

		newstats.st.st_size = info->end_of_file_info.in.size;
		break;

	case RAW_SFILEINFO_MODE_INFORMATION:
		if (info->mode_information.in.mode != 0 &&
		    info->mode_information.in.mode != 2 &&
		    info->mode_information.in.mode != 4 &&
		    info->mode_information.in.mode != 6) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		return NT_STATUS_OK;

	case RAW_SFILEINFO_RENAME_INFORMATION:
	case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
		status = pvfs_locking_key(name, name, &odb_locking_key);
		NT_STATUS_NOT_OK_RETURN(status);
		status = pvfs_setfileinfo_rename(pvfs, req, name, -1,
						 &odb_locking_key, info);
		NT_STATUS_NOT_OK_RETURN(status);
		return NT_STATUS_OK;

	case RAW_SFILEINFO_DISPOSITION_INFO:
	case RAW_SFILEINFO_DISPOSITION_INFORMATION:
	case RAW_SFILEINFO_POSITION_INFORMATION:
		return NT_STATUS_OK;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	/* possibly change the file size */
	if (newstats.st.st_size != name->st.st_size) {
		if (name->stream_name) {
			status = pvfs_stream_truncate(pvfs, name, -1, newstats.st.st_size);
			if (!NT_STATUS_IS_OK(status)) {
				return status;
			}
		} else if (truncate(name->full_name, newstats.st.st_size) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
		change_mask |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES;
	}

	/* possibly change the file timestamps */
	if (newstats.dos.create_time != name->dos.create_time) {
		change_mask |= FILE_NOTIFY_CHANGE_CREATION;
	}
	if (newstats.dos.access_time != name->dos.access_time) {
		change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
	}
	if (newstats.dos.write_time != name->dos.write_time) {
		change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
	}
	if ((change_mask & FILE_NOTIFY_CHANGE_LAST_ACCESS) ||
	    (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE)) {
		struct timeval tv[2];

		nttime_to_timeval(&tv[0], newstats.dos.access_time);
		nttime_to_timeval(&tv[1], newstats.dos.write_time);

		if (!timeval_is_zero(&tv[0]) || !timeval_is_zero(&tv[1])) {
			if (utimes(name->full_name, tv) == -1) {
				DEBUG(0,("pvfs_setpathinfo: utimes() failed '%s' - %s\n",
					 name->full_name, strerror(errno)));
				return pvfs_map_errno(pvfs, errno);
			}
		}
	}
	if (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE) {
		if (lck == NULL) {
			DATA_BLOB lkey;
			status = pvfs_locking_key(name, name, &lkey);
			NT_STATUS_NOT_OK_RETURN(status);

			lck = odb_lock(req, pvfs->odb_context, &lkey);
			data_blob_free(&lkey);
			if (lck == NULL) {
				DEBUG(0,("Unable to lock opendb for write time update\n"));
				return NT_STATUS_INTERNAL_ERROR;
			}
		}

		status = odb_set_write_time(lck, newstats.dos.write_time, true);
		if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
			/* it could be that nobody has opened the file */
		} else if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0,("Unable to update write time: %s\n",
				nt_errstr(status)));
			return status;
		}
	}

	/* possibly change the attribute */
	newstats.dos.attrib |= (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY);
	if (newstats.dos.attrib != name->dos.attrib) {
		mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
		if (pvfs_sys_chmod(pvfs, name->full_name, mode, name->allow_override) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
		change_mask |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
	}

	*name = newstats;

	if (change_mask != 0) {
		notify_trigger(pvfs->notify_context, 
			       NOTIFY_ACTION_MODIFIED, 
			       change_mask,
			       name->full_name);
	}

	return pvfs_dosattrib_save(pvfs, name, -1);
}