]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/doc_tombstone.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / vfs / doc_tombstone.c
CommitLineData
39037602
A
1/*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29// -- Document ID Tombstone Support --
30
31#include <stdint.h>
32#include <sys/resource.h>
33#include <sys/signal.h>
34#include <sys/vfs_context.h>
35#include <sys/doc_tombstone.h>
36#include <sys/vnode_internal.h>
37#include <sys/fsevents.h>
38#include <kern/thread.h>
39#include <kern/kalloc.h>
40#include <string.h>
41
42//
43// This function gets the doc_tombstone structure for the
44// current thread. If the thread doesn't have one, the
45// structure is allocated.
46//
47struct doc_tombstone *
48doc_tombstone_get(void)
49{
50 struct uthread *ut;
51 ut = get_bsdthread_info(current_thread());
52
53 if (ut->t_tombstone == NULL) {
f427ee49
A
54 ut->t_tombstone = kalloc_flags(sizeof(struct doc_tombstone),
55 Z_WAITOK | Z_ZERO);
39037602
A
56 }
57
58 return ut->t_tombstone;
59}
60
61//
62// This routine clears out the current tombstone for the
63// current thread and if necessary passes the doc-id of
64// the tombstone on to the dst_cnode.
65//
66// The caller is responsible for generating the appropriate
67// fsevents.
68//
69void
70doc_tombstone_clear(struct doc_tombstone *ut, vnode_t *old_vpp)
71{
f427ee49 72 uint64_t old_id = ut->t_lastop_document_id;
39037602
A
73
74 ut->t_lastop_document_id = 0;
75 ut->t_lastop_parent = NULL;
76 ut->t_lastop_parent_vid = 0;
77 ut->t_lastop_filename[0] = '\0';
78
79 //
80 // If the lastop item is still the same and needs to be cleared,
81 // clear it. The following isn't ideal because the vnode might
82 // have been recycled.
83 //
84 if (old_vpp) {
85 *old_vpp = NULL;
86 if (old_id && ut->t_lastop_item
0a7de745 87 && vnode_vid(ut->t_lastop_item) == ut->t_lastop_item_vid) {
39037602
A
88 int res = vnode_get(ut->t_lastop_item);
89 if (!res) {
90 // Need to check vid again
91 if (vnode_vid(ut->t_lastop_item) == ut->t_lastop_item_vid
0a7de745 92 && !ISSET(ut->t_lastop_item->v_lflag, VL_TERMINATE)) {
39037602 93 *old_vpp = ut->t_lastop_item;
0a7de745 94 } else {
39037602 95 vnode_put(ut->t_lastop_item);
0a7de745 96 }
39037602
A
97 }
98 }
99 }
100
101 // last, clear these now that we're all done
102 ut->t_lastop_item = NULL;
103 ut->t_lastop_fileid = 0;
104 ut->t_lastop_item_vid = 0;
105}
106
107
108//
109// This function is used to filter out operations on temp
110// filenames. We have to filter out operations on certain
111// temp filenames to work-around questionable application
112// behavior from apps like Autocad that perform unusual
113// sequences of file system operations for a "safe save".
0a7de745
A
114bool
115doc_tombstone_should_ignore_name(const char *nameptr, int len)
39037602 116{
f427ee49 117 size_t real_len;
39037602 118 if (len == 0) {
f427ee49
A
119 real_len = strlen(nameptr);
120 } else {
121 real_len = (size_t)len;
39037602
A
122 }
123
0a7de745 124 if (strncmp(nameptr, "atmp", 4) == 0
f427ee49
A
125 || (real_len > 4 && strncmp(nameptr + real_len - 4, ".bak", 4) == 0)
126 || (real_len > 4 && strncmp(nameptr + real_len - 4, ".tmp", 4) == 0)) {
39037602
A
127 return true;
128 }
129
130 return false;
131}
132
133//
134// Decide if we need to save a tombstone or not. Normally we always
135// save a tombstone - but if there already is one and the name we're
136// given is an ignorable name, then we will not save a tombstone.
137//
0a7de745
A
138bool
139doc_tombstone_should_save(struct doc_tombstone *ut, struct vnode *vp,
140 struct componentname *cnp)
39037602
A
141{
142 if (cnp->cn_nameptr == NULL) {
143 return false;
144 }
145
146 if (ut->t_lastop_document_id && ut->t_lastop_item == vp
0a7de745 147 && doc_tombstone_should_ignore_name(cnp->cn_nameptr, cnp->cn_namelen)) {
39037602
A
148 return false;
149 }
150
151 return true;
152}
153
154//
155// This function saves a tombstone for the given vnode and name. The
156// tombstone represents the parent directory and name where the document
157// used to live and the document-id of that file. This info is recorded
158// in the doc_tombstone structure hanging off the uthread (which assumes
159// that all safe-save operations happen on the same thread).
160//
161// If later on the same parent/name combo comes back into existence then
162// we'll preserve the doc-id from this vnode onto the new vnode.
163//
164// The caller is responsible for generating the appropriate
165// fsevents.
166//
167void
168doc_tombstone_save(struct vnode *dvp, struct vnode *vp,
0a7de745
A
169 struct componentname *cnp, uint64_t doc_id,
170 ino64_t file_id)
39037602
A
171{
172 struct doc_tombstone *ut;
173 ut = doc_tombstone_get();
174
175 ut->t_lastop_parent = dvp;
176 ut->t_lastop_parent_vid = vnode_vid(dvp);
177 ut->t_lastop_fileid = file_id;
178 ut->t_lastop_item = vp;
179 ut->t_lastop_item_vid = vp ? vnode_vid(vp) : 0;
0a7de745 180 ut->t_lastop_document_id = doc_id;
39037602
A
181
182 strlcpy((char *)&ut->t_lastop_filename[0], cnp->cn_nameptr, sizeof(ut->t_lastop_filename));
183}