inode.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/pagemap.h>
  24. #include <asm/div64.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_fs_sb.h"
  31. static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
  32. {
  33. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  34. switch (inode->i_mode & S_IFMT) {
  35. case S_IFREG:
  36. inode->i_op = &cifs_file_inode_ops;
  37. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  38. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  39. inode->i_fop = &cifs_file_direct_nobrl_ops;
  40. else
  41. inode->i_fop = &cifs_file_direct_ops;
  42. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  43. inode->i_fop = &cifs_file_nobrl_ops;
  44. else { /* not direct, send byte range locks */
  45. inode->i_fop = &cifs_file_ops;
  46. }
  47. /* check if server can support readpages */
  48. if (cifs_sb->tcon->ses->server->maxBuf <
  49. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  50. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  51. else
  52. inode->i_data.a_ops = &cifs_addr_ops;
  53. break;
  54. case S_IFDIR:
  55. #ifdef CONFIG_CIFS_DFS_UPCALL
  56. if (is_dfs_referral) {
  57. inode->i_op = &cifs_dfs_referral_inode_operations;
  58. } else {
  59. #else /* NO DFS support, treat as a directory */
  60. {
  61. #endif
  62. inode->i_op = &cifs_dir_inode_ops;
  63. inode->i_fop = &cifs_dir_ops;
  64. }
  65. break;
  66. case S_IFLNK:
  67. inode->i_op = &cifs_symlink_inode_ops;
  68. break;
  69. default:
  70. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  71. break;
  72. }
  73. }
  74. /* check inode attributes against fattr. If they don't match, tag the
  75. * inode for cache invalidation
  76. */
  77. static void
  78. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  79. {
  80. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  81. cFYI(1, ("%s: revalidating inode %llu", __func__, cifs_i->uniqueid));
  82. if (inode->i_state & I_NEW) {
  83. cFYI(1, ("%s: inode %llu is new", __func__, cifs_i->uniqueid));
  84. return;
  85. }
  86. /* don't bother with revalidation if we have an oplock */
  87. if (cifs_i->clientCanCacheRead) {
  88. cFYI(1, ("%s: inode %llu is oplocked", __func__,
  89. cifs_i->uniqueid));
  90. return;
  91. }
  92. /* revalidate if mtime or size have changed */
  93. if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  94. cifs_i->server_eof == fattr->cf_eof) {
  95. cFYI(1, ("%s: inode %llu is unchanged", __func__,
  96. cifs_i->uniqueid));
  97. return;
  98. }
  99. cFYI(1, ("%s: invalidating inode %llu mapping", __func__,
  100. cifs_i->uniqueid));
  101. cifs_i->invalid_mapping = true;
  102. }
  103. /* populate an inode with info from a cifs_fattr struct */
  104. void
  105. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  106. {
  107. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  108. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  109. unsigned long oldtime = cifs_i->time;
  110. cifs_revalidate_cache(inode, fattr);
  111. inode->i_atime = fattr->cf_atime;
  112. inode->i_mtime = fattr->cf_mtime;
  113. inode->i_ctime = fattr->cf_ctime;
  114. inode->i_rdev = fattr->cf_rdev;
  115. inode->i_nlink = fattr->cf_nlink;
  116. inode->i_uid = fattr->cf_uid;
  117. inode->i_gid = fattr->cf_gid;
  118. /* if dynperm is set, don't clobber existing mode */
  119. if (inode->i_state & I_NEW ||
  120. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  121. inode->i_mode = fattr->cf_mode;
  122. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  123. cifs_i->uniqueid = fattr->cf_uniqueid;
  124. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  125. cifs_i->time = 0;
  126. else
  127. cifs_i->time = jiffies;
  128. cFYI(1, ("inode 0x%p old_time=%ld new_time=%ld", inode,
  129. oldtime, cifs_i->time));
  130. cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
  131. cifs_i->server_eof = fattr->cf_eof;
  132. /*
  133. * Can't safely change the file size here if the client is writing to
  134. * it due to potential races.
  135. */
  136. spin_lock(&inode->i_lock);
  137. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  138. i_size_write(inode, fattr->cf_eof);
  139. /*
  140. * i_blocks is not related to (i_size / i_blksize),
  141. * but instead 512 byte (2**9) size is required for
  142. * calculating num blocks.
  143. */
  144. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  145. }
  146. spin_unlock(&inode->i_lock);
  147. cifs_set_ops(inode, fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL);
  148. }
  149. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  150. void
  151. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  152. struct cifs_sb_info *cifs_sb)
  153. {
  154. memset(fattr, 0, sizeof(*fattr));
  155. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  156. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  157. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  158. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  159. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  160. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  161. fattr->cf_mode = le64_to_cpu(info->Permissions);
  162. /*
  163. * Since we set the inode type below we need to mask off
  164. * to avoid strange results if bits set above.
  165. */
  166. fattr->cf_mode &= ~S_IFMT;
  167. switch (le32_to_cpu(info->Type)) {
  168. case UNIX_FILE:
  169. fattr->cf_mode |= S_IFREG;
  170. fattr->cf_dtype = DT_REG;
  171. break;
  172. case UNIX_SYMLINK:
  173. fattr->cf_mode |= S_IFLNK;
  174. fattr->cf_dtype = DT_LNK;
  175. break;
  176. case UNIX_DIR:
  177. fattr->cf_mode |= S_IFDIR;
  178. fattr->cf_dtype = DT_DIR;
  179. break;
  180. case UNIX_CHARDEV:
  181. fattr->cf_mode |= S_IFCHR;
  182. fattr->cf_dtype = DT_CHR;
  183. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  184. le64_to_cpu(info->DevMinor) & MINORMASK);
  185. break;
  186. case UNIX_BLOCKDEV:
  187. fattr->cf_mode |= S_IFBLK;
  188. fattr->cf_dtype = DT_BLK;
  189. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  190. le64_to_cpu(info->DevMinor) & MINORMASK);
  191. break;
  192. case UNIX_FIFO:
  193. fattr->cf_mode |= S_IFIFO;
  194. fattr->cf_dtype = DT_FIFO;
  195. break;
  196. case UNIX_SOCKET:
  197. fattr->cf_mode |= S_IFSOCK;
  198. fattr->cf_dtype = DT_SOCK;
  199. break;
  200. default:
  201. /* safest to call it a file if we do not know */
  202. fattr->cf_mode |= S_IFREG;
  203. fattr->cf_dtype = DT_REG;
  204. cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
  205. break;
  206. }
  207. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  208. fattr->cf_uid = cifs_sb->mnt_uid;
  209. else
  210. fattr->cf_uid = le64_to_cpu(info->Uid);
  211. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  212. fattr->cf_gid = cifs_sb->mnt_gid;
  213. else
  214. fattr->cf_gid = le64_to_cpu(info->Gid);
  215. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  216. }
  217. /*
  218. * Fill a cifs_fattr struct with fake inode info.
  219. *
  220. * Needed to setup cifs_fattr data for the directory which is the
  221. * junction to the new submount (ie to setup the fake directory
  222. * which represents a DFS referral).
  223. */
  224. static void
  225. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  226. {
  227. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  228. cFYI(1, ("creating fake fattr for DFS referral"));
  229. memset(fattr, 0, sizeof(*fattr));
  230. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  231. fattr->cf_uid = cifs_sb->mnt_uid;
  232. fattr->cf_gid = cifs_sb->mnt_gid;
  233. fattr->cf_atime = CURRENT_TIME;
  234. fattr->cf_ctime = CURRENT_TIME;
  235. fattr->cf_mtime = CURRENT_TIME;
  236. fattr->cf_nlink = 2;
  237. fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
  238. }
  239. int cifs_get_inode_info_unix(struct inode **pinode,
  240. const unsigned char *full_path,
  241. struct super_block *sb, int xid)
  242. {
  243. int rc;
  244. FILE_UNIX_BASIC_INFO find_data;
  245. struct cifs_fattr fattr;
  246. struct cifsTconInfo *tcon;
  247. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  248. tcon = cifs_sb->tcon;
  249. cFYI(1, ("Getting info on %s", full_path));
  250. /* could have done a find first instead but this returns more info */
  251. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  252. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  253. CIFS_MOUNT_MAP_SPECIAL_CHR);
  254. if (!rc) {
  255. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  256. } else if (rc == -EREMOTE) {
  257. cifs_create_dfs_fattr(&fattr, sb);
  258. rc = 0;
  259. } else {
  260. return rc;
  261. }
  262. if (*pinode == NULL) {
  263. /* get new inode */
  264. *pinode = cifs_iget(sb, &fattr);
  265. if (!*pinode)
  266. rc = -ENOMEM;
  267. } else {
  268. /* we already have inode, update it */
  269. cifs_fattr_to_inode(*pinode, &fattr);
  270. }
  271. return rc;
  272. }
  273. static int
  274. cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path,
  275. struct cifs_sb_info *cifs_sb, int xid)
  276. {
  277. int rc;
  278. int oplock = 0;
  279. __u16 netfid;
  280. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  281. char buf[24];
  282. unsigned int bytes_read;
  283. char *pbuf;
  284. pbuf = buf;
  285. fattr->cf_mode &= ~S_IFMT;
  286. if (fattr->cf_eof == 0) {
  287. fattr->cf_mode |= S_IFIFO;
  288. fattr->cf_dtype = DT_FIFO;
  289. return 0;
  290. } else if (fattr->cf_eof < 8) {
  291. fattr->cf_mode |= S_IFREG;
  292. fattr->cf_dtype = DT_REG;
  293. return -EINVAL; /* EOPNOTSUPP? */
  294. }
  295. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  296. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  297. cifs_sb->local_nls,
  298. cifs_sb->mnt_cifs_flags &
  299. CIFS_MOUNT_MAP_SPECIAL_CHR);
  300. if (rc == 0) {
  301. int buf_type = CIFS_NO_BUFFER;
  302. /* Read header */
  303. rc = CIFSSMBRead(xid, pTcon, netfid,
  304. 24 /* length */, 0 /* offset */,
  305. &bytes_read, &pbuf, &buf_type);
  306. if ((rc == 0) && (bytes_read >= 8)) {
  307. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  308. cFYI(1, ("Block device"));
  309. fattr->cf_mode |= S_IFBLK;
  310. fattr->cf_dtype = DT_BLK;
  311. if (bytes_read == 24) {
  312. /* we have enough to decode dev num */
  313. __u64 mjr; /* major */
  314. __u64 mnr; /* minor */
  315. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  316. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  317. fattr->cf_rdev = MKDEV(mjr, mnr);
  318. }
  319. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  320. cFYI(1, ("Char device"));
  321. fattr->cf_mode |= S_IFCHR;
  322. fattr->cf_dtype = DT_CHR;
  323. if (bytes_read == 24) {
  324. /* we have enough to decode dev num */
  325. __u64 mjr; /* major */
  326. __u64 mnr; /* minor */
  327. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  328. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  329. fattr->cf_rdev = MKDEV(mjr, mnr);
  330. }
  331. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  332. cFYI(1, ("Symlink"));
  333. fattr->cf_mode |= S_IFLNK;
  334. fattr->cf_dtype = DT_LNK;
  335. } else {
  336. fattr->cf_mode |= S_IFREG; /* file? */
  337. fattr->cf_dtype = DT_REG;
  338. rc = -EOPNOTSUPP;
  339. }
  340. } else {
  341. fattr->cf_mode |= S_IFREG; /* then it is a file */
  342. fattr->cf_dtype = DT_REG;
  343. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  344. }
  345. CIFSSMBClose(xid, pTcon, netfid);
  346. }
  347. return rc;
  348. }
  349. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  350. /*
  351. * Fetch mode bits as provided by SFU.
  352. *
  353. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  354. */
  355. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  356. struct cifs_sb_info *cifs_sb, int xid)
  357. {
  358. #ifdef CONFIG_CIFS_XATTR
  359. ssize_t rc;
  360. char ea_value[4];
  361. __u32 mode;
  362. rc = CIFSSMBQAllEAs(xid, cifs_sb->tcon, path, "SETFILEBITS",
  363. ea_value, 4 /* size of buf */, cifs_sb->local_nls,
  364. cifs_sb->mnt_cifs_flags &
  365. CIFS_MOUNT_MAP_SPECIAL_CHR);
  366. if (rc < 0)
  367. return (int)rc;
  368. else if (rc > 3) {
  369. mode = le32_to_cpu(*((__le32 *)ea_value));
  370. fattr->cf_mode &= ~SFBITS_MASK;
  371. cFYI(1, ("special bits 0%o org mode 0%o", mode,
  372. fattr->cf_mode));
  373. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  374. cFYI(1, ("special mode bits 0%o", mode));
  375. }
  376. return 0;
  377. #else
  378. return -EOPNOTSUPP;
  379. #endif
  380. }
  381. /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
  382. static void
  383. cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
  384. struct cifs_sb_info *cifs_sb, bool adjust_tz)
  385. {
  386. memset(fattr, 0, sizeof(*fattr));
  387. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  388. if (info->DeletePending)
  389. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  390. if (info->LastAccessTime)
  391. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  392. else
  393. fattr->cf_atime = CURRENT_TIME;
  394. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  395. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  396. if (adjust_tz) {
  397. fattr->cf_ctime.tv_sec += cifs_sb->tcon->ses->server->timeAdj;
  398. fattr->cf_mtime.tv_sec += cifs_sb->tcon->ses->server->timeAdj;
  399. }
  400. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  401. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  402. if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  403. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  404. fattr->cf_dtype = DT_DIR;
  405. } else {
  406. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  407. fattr->cf_dtype = DT_REG;
  408. /* clear write bits if ATTR_READONLY is set */
  409. if (fattr->cf_cifsattrs & ATTR_READONLY)
  410. fattr->cf_mode &= ~(S_IWUGO);
  411. }
  412. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  413. fattr->cf_uid = cifs_sb->mnt_uid;
  414. fattr->cf_gid = cifs_sb->mnt_gid;
  415. }
  416. int cifs_get_inode_info(struct inode **pinode,
  417. const unsigned char *full_path, FILE_ALL_INFO *pfindData,
  418. struct super_block *sb, int xid, const __u16 *pfid)
  419. {
  420. int rc = 0, tmprc;
  421. struct cifsTconInfo *pTcon;
  422. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  423. char *buf = NULL;
  424. bool adjustTZ = false;
  425. struct cifs_fattr fattr;
  426. pTcon = cifs_sb->tcon;
  427. cFYI(1, ("Getting info on %s", full_path));
  428. if ((pfindData == NULL) && (*pinode != NULL)) {
  429. if (CIFS_I(*pinode)->clientCanCacheRead) {
  430. cFYI(1, ("No need to revalidate cached inode sizes"));
  431. return rc;
  432. }
  433. }
  434. /* if file info not passed in then get it from server */
  435. if (pfindData == NULL) {
  436. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  437. if (buf == NULL)
  438. return -ENOMEM;
  439. pfindData = (FILE_ALL_INFO *)buf;
  440. /* could do find first instead but this returns more info */
  441. rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
  442. 0 /* not legacy */,
  443. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  444. CIFS_MOUNT_MAP_SPECIAL_CHR);
  445. /* BB optimize code so we do not make the above call
  446. when server claims no NT SMB support and the above call
  447. failed at least once - set flag in tcon or mount */
  448. if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
  449. rc = SMBQueryInformation(xid, pTcon, full_path,
  450. pfindData, cifs_sb->local_nls,
  451. cifs_sb->mnt_cifs_flags &
  452. CIFS_MOUNT_MAP_SPECIAL_CHR);
  453. adjustTZ = true;
  454. }
  455. }
  456. if (!rc) {
  457. cifs_all_info_to_fattr(&fattr, (FILE_ALL_INFO *) pfindData,
  458. cifs_sb, adjustTZ);
  459. } else if (rc == -EREMOTE) {
  460. cifs_create_dfs_fattr(&fattr, sb);
  461. rc = 0;
  462. } else {
  463. goto cgii_exit;
  464. }
  465. /*
  466. * If an inode wasn't passed in, then get the inode number
  467. *
  468. * Is an i_ino of zero legal? Can we use that to check if the server
  469. * supports returning inode numbers? Are there other sanity checks we
  470. * can use to ensure that the server is really filling in that field?
  471. *
  472. * We can not use the IndexNumber field by default from Windows or
  473. * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
  474. * CIFS spec claims that this value is unique within the scope of a
  475. * share, and the windows docs hint that it's actually unique
  476. * per-machine.
  477. *
  478. * There may be higher info levels that work but are there Windows
  479. * server or network appliances for which IndexNumber field is not
  480. * guaranteed unique?
  481. */
  482. if (*pinode == NULL) {
  483. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  484. int rc1 = 0;
  485. rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
  486. full_path, &fattr.cf_uniqueid,
  487. cifs_sb->local_nls,
  488. cifs_sb->mnt_cifs_flags &
  489. CIFS_MOUNT_MAP_SPECIAL_CHR);
  490. if (rc1 || !fattr.cf_uniqueid) {
  491. cFYI(1, ("GetSrvInodeNum rc %d", rc1));
  492. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  493. cifs_autodisable_serverino(cifs_sb);
  494. }
  495. } else {
  496. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  497. }
  498. } else {
  499. fattr.cf_uniqueid = CIFS_I(*pinode)->uniqueid;
  500. }
  501. /* query for SFU type info if supported and needed */
  502. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  503. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  504. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  505. if (tmprc)
  506. cFYI(1, ("cifs_sfu_type failed: %d", tmprc));
  507. }
  508. #ifdef CONFIG_CIFS_EXPERIMENTAL
  509. /* fill in 0777 bits from ACL */
  510. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  511. cFYI(1, ("Getting mode bits from ACL"));
  512. cifs_acl_to_fattr(cifs_sb, &fattr, *pinode, full_path, pfid);
  513. }
  514. #endif
  515. /* fill in remaining high mode bits e.g. SUID, VTX */
  516. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  517. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  518. if (!*pinode) {
  519. *pinode = cifs_iget(sb, &fattr);
  520. if (!*pinode)
  521. rc = -ENOMEM;
  522. } else {
  523. cifs_fattr_to_inode(*pinode, &fattr);
  524. }
  525. cgii_exit:
  526. kfree(buf);
  527. return rc;
  528. }
  529. static const struct inode_operations cifs_ipc_inode_ops = {
  530. .lookup = cifs_lookup,
  531. };
  532. char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb)
  533. {
  534. int pplen = cifs_sb->prepathlen;
  535. int dfsplen;
  536. char *full_path = NULL;
  537. /* if no prefix path, simply set path to the root of share to "" */
  538. if (pplen == 0) {
  539. full_path = kmalloc(1, GFP_KERNEL);
  540. if (full_path)
  541. full_path[0] = 0;
  542. return full_path;
  543. }
  544. if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
  545. dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
  546. else
  547. dfsplen = 0;
  548. full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
  549. if (full_path == NULL)
  550. return full_path;
  551. if (dfsplen) {
  552. strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
  553. /* switch slash direction in prepath depending on whether
  554. * windows or posix style path names
  555. */
  556. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  557. int i;
  558. for (i = 0; i < dfsplen; i++) {
  559. if (full_path[i] == '\\')
  560. full_path[i] = '/';
  561. }
  562. }
  563. }
  564. strncpy(full_path + dfsplen, cifs_sb->prepath, pplen);
  565. full_path[dfsplen + pplen] = 0; /* add trailing null */
  566. return full_path;
  567. }
  568. static int
  569. cifs_find_inode(struct inode *inode, void *opaque)
  570. {
  571. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  572. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  573. return 0;
  574. return 1;
  575. }
  576. static int
  577. cifs_init_inode(struct inode *inode, void *opaque)
  578. {
  579. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  580. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  581. return 0;
  582. }
  583. /* Given fattrs, get a corresponding inode */
  584. struct inode *
  585. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  586. {
  587. unsigned long hash;
  588. struct inode *inode;
  589. cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid));
  590. /* hash down to 32-bits on 32-bit arch */
  591. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  592. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  593. /* we have fattrs in hand, update the inode */
  594. if (inode) {
  595. cifs_fattr_to_inode(inode, fattr);
  596. if (sb->s_flags & MS_NOATIME)
  597. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  598. if (inode->i_state & I_NEW) {
  599. inode->i_ino = hash;
  600. unlock_new_inode(inode);
  601. }
  602. }
  603. return inode;
  604. }
  605. /* gets root inode */
  606. struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino)
  607. {
  608. int xid;
  609. struct cifs_sb_info *cifs_sb;
  610. struct inode *inode = NULL;
  611. long rc;
  612. char *full_path;
  613. cifs_sb = CIFS_SB(sb);
  614. full_path = cifs_build_path_to_root(cifs_sb);
  615. if (full_path == NULL)
  616. return ERR_PTR(-ENOMEM);
  617. xid = GetXid();
  618. if (cifs_sb->tcon->unix_ext)
  619. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  620. else
  621. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  622. xid, NULL);
  623. if (!inode)
  624. return ERR_PTR(-ENOMEM);
  625. if (rc && cifs_sb->tcon->ipc) {
  626. cFYI(1, ("ipc connection - fake read inode"));
  627. inode->i_mode |= S_IFDIR;
  628. inode->i_nlink = 2;
  629. inode->i_op = &cifs_ipc_inode_ops;
  630. inode->i_fop = &simple_dir_operations;
  631. inode->i_uid = cifs_sb->mnt_uid;
  632. inode->i_gid = cifs_sb->mnt_gid;
  633. } else if (rc) {
  634. kfree(full_path);
  635. _FreeXid(xid);
  636. iget_failed(inode);
  637. return ERR_PTR(rc);
  638. }
  639. kfree(full_path);
  640. /* can not call macro FreeXid here since in a void func
  641. * TODO: This is no longer true
  642. */
  643. _FreeXid(xid);
  644. return inode;
  645. }
  646. static int
  647. cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
  648. char *full_path, __u32 dosattr)
  649. {
  650. int rc;
  651. int oplock = 0;
  652. __u16 netfid;
  653. __u32 netpid;
  654. bool set_time = false;
  655. struct cifsFileInfo *open_file;
  656. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  657. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  658. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  659. FILE_BASIC_INFO info_buf;
  660. if (attrs == NULL)
  661. return -EINVAL;
  662. if (attrs->ia_valid & ATTR_ATIME) {
  663. set_time = true;
  664. info_buf.LastAccessTime =
  665. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  666. } else
  667. info_buf.LastAccessTime = 0;
  668. if (attrs->ia_valid & ATTR_MTIME) {
  669. set_time = true;
  670. info_buf.LastWriteTime =
  671. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  672. } else
  673. info_buf.LastWriteTime = 0;
  674. /*
  675. * Samba throws this field away, but windows may actually use it.
  676. * Do not set ctime unless other time stamps are changed explicitly
  677. * (i.e. by utimes()) since we would then have a mix of client and
  678. * server times.
  679. */
  680. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  681. cFYI(1, ("CIFS - CTIME changed"));
  682. info_buf.ChangeTime =
  683. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  684. } else
  685. info_buf.ChangeTime = 0;
  686. info_buf.CreationTime = 0; /* don't change */
  687. info_buf.Attributes = cpu_to_le32(dosattr);
  688. /*
  689. * If the file is already open for write, just use that fileid
  690. */
  691. open_file = find_writable_file(cifsInode);
  692. if (open_file) {
  693. netfid = open_file->netfid;
  694. netpid = open_file->pid;
  695. goto set_via_filehandle;
  696. }
  697. /*
  698. * NT4 apparently returns success on this call, but it doesn't
  699. * really work.
  700. */
  701. if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
  702. rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
  703. &info_buf, cifs_sb->local_nls,
  704. cifs_sb->mnt_cifs_flags &
  705. CIFS_MOUNT_MAP_SPECIAL_CHR);
  706. if (rc == 0) {
  707. cifsInode->cifsAttrs = dosattr;
  708. goto out;
  709. } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
  710. goto out;
  711. }
  712. cFYI(1, ("calling SetFileInfo since SetPathInfo for "
  713. "times not supported by this server"));
  714. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
  715. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  716. CREATE_NOT_DIR, &netfid, &oplock,
  717. NULL, cifs_sb->local_nls,
  718. cifs_sb->mnt_cifs_flags &
  719. CIFS_MOUNT_MAP_SPECIAL_CHR);
  720. if (rc != 0) {
  721. if (rc == -EIO)
  722. rc = -EINVAL;
  723. goto out;
  724. }
  725. netpid = current->tgid;
  726. set_via_filehandle:
  727. rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
  728. if (!rc)
  729. cifsInode->cifsAttrs = dosattr;
  730. if (open_file == NULL)
  731. CIFSSMBClose(xid, pTcon, netfid);
  732. else
  733. cifsFileInfo_put(open_file);
  734. out:
  735. return rc;
  736. }
  737. /*
  738. * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  739. * and rename it to a random name that hopefully won't conflict with
  740. * anything else.
  741. */
  742. static int
  743. cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
  744. {
  745. int oplock = 0;
  746. int rc;
  747. __u16 netfid;
  748. struct inode *inode = dentry->d_inode;
  749. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  750. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  751. struct cifsTconInfo *tcon = cifs_sb->tcon;
  752. __u32 dosattr, origattr;
  753. FILE_BASIC_INFO *info_buf = NULL;
  754. rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
  755. DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
  756. &netfid, &oplock, NULL, cifs_sb->local_nls,
  757. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  758. if (rc != 0)
  759. goto out;
  760. origattr = cifsInode->cifsAttrs;
  761. if (origattr == 0)
  762. origattr |= ATTR_NORMAL;
  763. dosattr = origattr & ~ATTR_READONLY;
  764. if (dosattr == 0)
  765. dosattr |= ATTR_NORMAL;
  766. dosattr |= ATTR_HIDDEN;
  767. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  768. if (dosattr != origattr) {
  769. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  770. if (info_buf == NULL) {
  771. rc = -ENOMEM;
  772. goto out_close;
  773. }
  774. info_buf->Attributes = cpu_to_le32(dosattr);
  775. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  776. current->tgid);
  777. /* although we would like to mark the file hidden
  778. if that fails we will still try to rename it */
  779. if (rc != 0)
  780. cifsInode->cifsAttrs = dosattr;
  781. else
  782. dosattr = origattr; /* since not able to change them */
  783. }
  784. /* rename the file */
  785. rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
  786. cifs_sb->mnt_cifs_flags &
  787. CIFS_MOUNT_MAP_SPECIAL_CHR);
  788. if (rc != 0) {
  789. rc = -ETXTBSY;
  790. goto undo_setattr;
  791. }
  792. /* try to set DELETE_ON_CLOSE */
  793. if (!cifsInode->delete_pending) {
  794. rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
  795. current->tgid);
  796. /*
  797. * some samba versions return -ENOENT when we try to set the
  798. * file disposition here. Likely a samba bug, but work around
  799. * it for now. This means that some cifsXXX files may hang
  800. * around after they shouldn't.
  801. *
  802. * BB: remove this hack after more servers have the fix
  803. */
  804. if (rc == -ENOENT)
  805. rc = 0;
  806. else if (rc != 0) {
  807. rc = -ETXTBSY;
  808. goto undo_rename;
  809. }
  810. cifsInode->delete_pending = true;
  811. }
  812. out_close:
  813. CIFSSMBClose(xid, tcon, netfid);
  814. out:
  815. kfree(info_buf);
  816. return rc;
  817. /*
  818. * reset everything back to the original state. Don't bother
  819. * dealing with errors here since we can't do anything about
  820. * them anyway.
  821. */
  822. undo_rename:
  823. CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
  824. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  825. CIFS_MOUNT_MAP_SPECIAL_CHR);
  826. undo_setattr:
  827. if (dosattr != origattr) {
  828. info_buf->Attributes = cpu_to_le32(origattr);
  829. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  830. current->tgid))
  831. cifsInode->cifsAttrs = origattr;
  832. }
  833. goto out_close;
  834. }
  835. /*
  836. * If dentry->d_inode is null (usually meaning the cached dentry
  837. * is a negative dentry) then we would attempt a standard SMB delete, but
  838. * if that fails we can not attempt the fall back mechanisms on EACCESS
  839. * but will return the EACCESS to the caller. Note that the VFS does not call
  840. * unlink on negative dentries currently.
  841. */
  842. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  843. {
  844. int rc = 0;
  845. int xid;
  846. char *full_path = NULL;
  847. struct inode *inode = dentry->d_inode;
  848. struct cifsInodeInfo *cifs_inode;
  849. struct super_block *sb = dir->i_sb;
  850. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  851. struct cifsTconInfo *tcon = cifs_sb->tcon;
  852. struct iattr *attrs = NULL;
  853. __u32 dosattr = 0, origattr = 0;
  854. cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
  855. xid = GetXid();
  856. /* Unlink can be called from rename so we can not take the
  857. * sb->s_vfs_rename_mutex here */
  858. full_path = build_path_from_dentry(dentry);
  859. if (full_path == NULL) {
  860. rc = -ENOMEM;
  861. FreeXid(xid);
  862. return rc;
  863. }
  864. if ((tcon->ses->capabilities & CAP_UNIX) &&
  865. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  866. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  867. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  868. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  869. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  870. cFYI(1, ("posix del rc %d", rc));
  871. if ((rc == 0) || (rc == -ENOENT))
  872. goto psx_del_no_retry;
  873. }
  874. retry_std_delete:
  875. rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
  876. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  877. psx_del_no_retry:
  878. if (!rc) {
  879. if (inode)
  880. drop_nlink(inode);
  881. } else if (rc == -ENOENT) {
  882. d_drop(dentry);
  883. } else if (rc == -ETXTBSY) {
  884. rc = cifs_rename_pending_delete(full_path, dentry, xid);
  885. if (rc == 0)
  886. drop_nlink(inode);
  887. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  888. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  889. if (attrs == NULL) {
  890. rc = -ENOMEM;
  891. goto out_reval;
  892. }
  893. /* try to reset dos attributes */
  894. cifs_inode = CIFS_I(inode);
  895. origattr = cifs_inode->cifsAttrs;
  896. if (origattr == 0)
  897. origattr |= ATTR_NORMAL;
  898. dosattr = origattr & ~ATTR_READONLY;
  899. if (dosattr == 0)
  900. dosattr |= ATTR_NORMAL;
  901. dosattr |= ATTR_HIDDEN;
  902. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  903. if (rc != 0)
  904. goto out_reval;
  905. goto retry_std_delete;
  906. }
  907. /* undo the setattr if we errored out and it's needed */
  908. if (rc != 0 && dosattr != 0)
  909. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  910. out_reval:
  911. if (inode) {
  912. cifs_inode = CIFS_I(inode);
  913. cifs_inode->time = 0; /* will force revalidate to get info
  914. when needed */
  915. inode->i_ctime = current_fs_time(sb);
  916. }
  917. dir->i_ctime = dir->i_mtime = current_fs_time(sb);
  918. cifs_inode = CIFS_I(dir);
  919. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  920. kfree(full_path);
  921. kfree(attrs);
  922. FreeXid(xid);
  923. return rc;
  924. }
  925. int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
  926. {
  927. int rc = 0, tmprc;
  928. int xid;
  929. struct cifs_sb_info *cifs_sb;
  930. struct cifsTconInfo *pTcon;
  931. char *full_path = NULL;
  932. struct inode *newinode = NULL;
  933. struct cifs_fattr fattr;
  934. cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
  935. xid = GetXid();
  936. cifs_sb = CIFS_SB(inode->i_sb);
  937. pTcon = cifs_sb->tcon;
  938. full_path = build_path_from_dentry(direntry);
  939. if (full_path == NULL) {
  940. rc = -ENOMEM;
  941. FreeXid(xid);
  942. return rc;
  943. }
  944. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  945. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  946. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  947. u32 oplock = 0;
  948. FILE_UNIX_BASIC_INFO *pInfo =
  949. kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  950. if (pInfo == NULL) {
  951. rc = -ENOMEM;
  952. goto mkdir_out;
  953. }
  954. mode &= ~current_umask();
  955. rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
  956. mode, NULL /* netfid */, pInfo, &oplock,
  957. full_path, cifs_sb->local_nls,
  958. cifs_sb->mnt_cifs_flags &
  959. CIFS_MOUNT_MAP_SPECIAL_CHR);
  960. if (rc == -EOPNOTSUPP) {
  961. kfree(pInfo);
  962. goto mkdir_retry_old;
  963. } else if (rc) {
  964. cFYI(1, ("posix mkdir returned 0x%x", rc));
  965. d_drop(direntry);
  966. } else {
  967. if (pInfo->Type == cpu_to_le32(-1)) {
  968. /* no return info, go query for it */
  969. kfree(pInfo);
  970. goto mkdir_get_info;
  971. }
  972. /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
  973. to set uid/gid */
  974. inc_nlink(inode);
  975. if (pTcon->nocase)
  976. direntry->d_op = &cifs_ci_dentry_ops;
  977. else
  978. direntry->d_op = &cifs_dentry_ops;
  979. cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb);
  980. newinode = cifs_iget(inode->i_sb, &fattr);
  981. if (!newinode) {
  982. kfree(pInfo);
  983. goto mkdir_get_info;
  984. }
  985. d_instantiate(direntry, newinode);
  986. #ifdef CONFIG_CIFS_DEBUG2
  987. cFYI(1, ("instantiated dentry %p %s to inode %p",
  988. direntry, direntry->d_name.name, newinode));
  989. if (newinode->i_nlink != 2)
  990. cFYI(1, ("unexpected number of links %d",
  991. newinode->i_nlink));
  992. #endif
  993. }
  994. kfree(pInfo);
  995. goto mkdir_out;
  996. }
  997. mkdir_retry_old:
  998. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  999. rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
  1000. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1001. if (rc) {
  1002. cFYI(1, ("cifs_mkdir returned 0x%x", rc));
  1003. d_drop(direntry);
  1004. } else {
  1005. mkdir_get_info:
  1006. inc_nlink(inode);
  1007. if (pTcon->unix_ext)
  1008. rc = cifs_get_inode_info_unix(&newinode, full_path,
  1009. inode->i_sb, xid);
  1010. else
  1011. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  1012. inode->i_sb, xid, NULL);
  1013. if (pTcon->nocase)
  1014. direntry->d_op = &cifs_ci_dentry_ops;
  1015. else
  1016. direntry->d_op = &cifs_dentry_ops;
  1017. d_instantiate(direntry, newinode);
  1018. /* setting nlink not necessary except in cases where we
  1019. * failed to get it from the server or was set bogus */
  1020. if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
  1021. direntry->d_inode->i_nlink = 2;
  1022. mode &= ~current_umask();
  1023. /* must turn on setgid bit if parent dir has it */
  1024. if (inode->i_mode & S_ISGID)
  1025. mode |= S_ISGID;
  1026. if (pTcon->unix_ext) {
  1027. struct cifs_unix_set_info_args args = {
  1028. .mode = mode,
  1029. .ctime = NO_CHANGE_64,
  1030. .atime = NO_CHANGE_64,
  1031. .mtime = NO_CHANGE_64,
  1032. .device = 0,
  1033. };
  1034. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1035. args.uid = (__u64)current_fsuid();
  1036. if (inode->i_mode & S_ISGID)
  1037. args.gid = (__u64)inode->i_gid;
  1038. else
  1039. args.gid = (__u64)current_fsgid();
  1040. } else {
  1041. args.uid = NO_CHANGE_64;
  1042. args.gid = NO_CHANGE_64;
  1043. }
  1044. CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  1045. cifs_sb->local_nls,
  1046. cifs_sb->mnt_cifs_flags &
  1047. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1048. } else {
  1049. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1050. (mode & S_IWUGO) == 0) {
  1051. FILE_BASIC_INFO pInfo;
  1052. struct cifsInodeInfo *cifsInode;
  1053. u32 dosattrs;
  1054. memset(&pInfo, 0, sizeof(pInfo));
  1055. cifsInode = CIFS_I(newinode);
  1056. dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
  1057. pInfo.Attributes = cpu_to_le32(dosattrs);
  1058. tmprc = CIFSSMBSetPathInfo(xid, pTcon,
  1059. full_path, &pInfo,
  1060. cifs_sb->local_nls,
  1061. cifs_sb->mnt_cifs_flags &
  1062. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1063. if (tmprc == 0)
  1064. cifsInode->cifsAttrs = dosattrs;
  1065. }
  1066. if (direntry->d_inode) {
  1067. if (cifs_sb->mnt_cifs_flags &
  1068. CIFS_MOUNT_DYNPERM)
  1069. direntry->d_inode->i_mode =
  1070. (mode | S_IFDIR);
  1071. if (cifs_sb->mnt_cifs_flags &
  1072. CIFS_MOUNT_SET_UID) {
  1073. direntry->d_inode->i_uid =
  1074. current_fsuid();
  1075. if (inode->i_mode & S_ISGID)
  1076. direntry->d_inode->i_gid =
  1077. inode->i_gid;
  1078. else
  1079. direntry->d_inode->i_gid =
  1080. current_fsgid();
  1081. }
  1082. }
  1083. }
  1084. }
  1085. mkdir_out:
  1086. kfree(full_path);
  1087. FreeXid(xid);
  1088. return rc;
  1089. }
  1090. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1091. {
  1092. int rc = 0;
  1093. int xid;
  1094. struct cifs_sb_info *cifs_sb;
  1095. struct cifsTconInfo *pTcon;
  1096. char *full_path = NULL;
  1097. struct cifsInodeInfo *cifsInode;
  1098. cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
  1099. xid = GetXid();
  1100. cifs_sb = CIFS_SB(inode->i_sb);
  1101. pTcon = cifs_sb->tcon;
  1102. full_path = build_path_from_dentry(direntry);
  1103. if (full_path == NULL) {
  1104. rc = -ENOMEM;
  1105. FreeXid(xid);
  1106. return rc;
  1107. }
  1108. rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
  1109. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1110. if (!rc) {
  1111. drop_nlink(inode);
  1112. spin_lock(&direntry->d_inode->i_lock);
  1113. i_size_write(direntry->d_inode, 0);
  1114. clear_nlink(direntry->d_inode);
  1115. spin_unlock(&direntry->d_inode->i_lock);
  1116. }
  1117. cifsInode = CIFS_I(direntry->d_inode);
  1118. cifsInode->time = 0; /* force revalidate to go get info when
  1119. needed */
  1120. cifsInode = CIFS_I(inode);
  1121. cifsInode->time = 0; /* force revalidate to get parent dir info
  1122. since cached search results now invalid */
  1123. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  1124. current_fs_time(inode->i_sb);
  1125. kfree(full_path);
  1126. FreeXid(xid);
  1127. return rc;
  1128. }
  1129. static int
  1130. cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
  1131. struct dentry *to_dentry, const char *toPath)
  1132. {
  1133. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1134. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1135. __u16 srcfid;
  1136. int oplock, rc;
  1137. /* try path-based rename first */
  1138. rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
  1139. cifs_sb->mnt_cifs_flags &
  1140. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1141. /*
  1142. * don't bother with rename by filehandle unless file is busy and
  1143. * source Note that cross directory moves do not work with
  1144. * rename by filehandle to various Windows servers.
  1145. */
  1146. if (rc == 0 || rc != -ETXTBSY)
  1147. return rc;
  1148. /* open the file to be renamed -- we need DELETE perms */
  1149. rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
  1150. CREATE_NOT_DIR, &srcfid, &oplock, NULL,
  1151. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1152. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1153. if (rc == 0) {
  1154. rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
  1155. (const char *) to_dentry->d_name.name,
  1156. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1157. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1158. CIFSSMBClose(xid, pTcon, srcfid);
  1159. }
  1160. return rc;
  1161. }
  1162. int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
  1163. struct inode *target_dir, struct dentry *target_dentry)
  1164. {
  1165. char *fromName = NULL;
  1166. char *toName = NULL;
  1167. struct cifs_sb_info *cifs_sb_source;
  1168. struct cifs_sb_info *cifs_sb_target;
  1169. struct cifsTconInfo *tcon;
  1170. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1171. FILE_UNIX_BASIC_INFO *info_buf_target;
  1172. int xid, rc, tmprc;
  1173. cifs_sb_target = CIFS_SB(target_dir->i_sb);
  1174. cifs_sb_source = CIFS_SB(source_dir->i_sb);
  1175. tcon = cifs_sb_source->tcon;
  1176. xid = GetXid();
  1177. /*
  1178. * BB: this might be allowed if same server, but different share.
  1179. * Consider adding support for this
  1180. */
  1181. if (tcon != cifs_sb_target->tcon) {
  1182. rc = -EXDEV;
  1183. goto cifs_rename_exit;
  1184. }
  1185. /*
  1186. * we already have the rename sem so we do not need to
  1187. * grab it again here to protect the path integrity
  1188. */
  1189. fromName = build_path_from_dentry(source_dentry);
  1190. if (fromName == NULL) {
  1191. rc = -ENOMEM;
  1192. goto cifs_rename_exit;
  1193. }
  1194. toName = build_path_from_dentry(target_dentry);
  1195. if (toName == NULL) {
  1196. rc = -ENOMEM;
  1197. goto cifs_rename_exit;
  1198. }
  1199. rc = cifs_do_rename(xid, source_dentry, fromName,
  1200. target_dentry, toName);
  1201. if (rc == -EEXIST && tcon->unix_ext) {
  1202. /*
  1203. * Are src and dst hardlinks of same inode? We can
  1204. * only tell with unix extensions enabled
  1205. */
  1206. info_buf_source =
  1207. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
  1208. GFP_KERNEL);
  1209. if (info_buf_source == NULL) {
  1210. rc = -ENOMEM;
  1211. goto cifs_rename_exit;
  1212. }
  1213. info_buf_target = info_buf_source + 1;
  1214. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
  1215. info_buf_source,
  1216. cifs_sb_source->local_nls,
  1217. cifs_sb_source->mnt_cifs_flags &
  1218. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1219. if (tmprc != 0)
  1220. goto unlink_target;
  1221. tmprc = CIFSSMBUnixQPathInfo(xid, tcon,
  1222. toName, info_buf_target,
  1223. cifs_sb_target->local_nls,
  1224. /* remap based on source sb */
  1225. cifs_sb_source->mnt_cifs_flags &
  1226. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1227. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1228. info_buf_target->UniqueId)) {
  1229. /* same file, POSIX says that this is a noop */
  1230. rc = 0;
  1231. goto cifs_rename_exit;
  1232. }
  1233. } /* else ... BB we could add the same check for Windows by
  1234. checking the UniqueId via FILE_INTERNAL_INFO */
  1235. unlink_target:
  1236. /* Try unlinking the target dentry if it's not negative */
  1237. if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
  1238. tmprc = cifs_unlink(target_dir, target_dentry);
  1239. if (tmprc)
  1240. goto cifs_rename_exit;
  1241. rc = cifs_do_rename(xid, source_dentry, fromName,
  1242. target_dentry, toName);
  1243. }
  1244. cifs_rename_exit:
  1245. kfree(info_buf_source);
  1246. kfree(fromName);
  1247. kfree(toName);
  1248. FreeXid(xid);
  1249. return rc;
  1250. }
  1251. static bool
  1252. cifs_inode_needs_reval(struct inode *inode)
  1253. {
  1254. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1255. if (cifs_i->clientCanCacheRead)
  1256. return false;
  1257. if (!lookupCacheEnabled)
  1258. return true;
  1259. if (cifs_i->time == 0)
  1260. return true;
  1261. /* FIXME: the actimeo should be tunable */
  1262. if (time_after_eq(jiffies, cifs_i->time + HZ))
  1263. return true;
  1264. return false;
  1265. }
  1266. /* check invalid_mapping flag and zap the cache if it's set */
  1267. static void
  1268. cifs_invalidate_mapping(struct inode *inode)
  1269. {
  1270. int rc;
  1271. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1272. cifs_i->invalid_mapping = false;
  1273. /* write back any cached data */
  1274. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  1275. rc = filemap_write_and_wait(inode->i_mapping);
  1276. if (rc)
  1277. cifs_i->write_behind_rc = rc;
  1278. }
  1279. invalidate_remote_inode(inode);
  1280. }
  1281. /* revalidate a dentry's inode attributes */
  1282. int cifs_revalidate_dentry(struct dentry *dentry)
  1283. {
  1284. int xid;
  1285. int rc = 0;
  1286. char *full_path = NULL;
  1287. struct inode *inode = dentry->d_inode;
  1288. struct super_block *sb = dentry->d_sb;
  1289. if (inode == NULL)
  1290. return -ENOENT;
  1291. xid = GetXid();
  1292. if (!cifs_inode_needs_reval(inode))
  1293. goto check_inval;
  1294. /* can not safely grab the rename sem here if rename calls revalidate
  1295. since that would deadlock */
  1296. full_path = build_path_from_dentry(dentry);
  1297. if (full_path == NULL) {
  1298. rc = -ENOMEM;
  1299. goto check_inval;
  1300. }
  1301. cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
  1302. "jiffies %ld", full_path, inode, inode->i_count.counter,
  1303. dentry, dentry->d_time, jiffies));
  1304. if (CIFS_SB(sb)->tcon->unix_ext)
  1305. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  1306. else
  1307. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  1308. xid, NULL);
  1309. check_inval:
  1310. if (CIFS_I(inode)->invalid_mapping)
  1311. cifs_invalidate_mapping(inode);
  1312. kfree(full_path);
  1313. FreeXid(xid);
  1314. return rc;
  1315. }
  1316. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1317. struct kstat *stat)
  1318. {
  1319. int err = cifs_revalidate_dentry(dentry);
  1320. if (!err) {
  1321. generic_fillattr(dentry->d_inode, stat);
  1322. stat->blksize = CIFS_MAX_MSGSIZE;
  1323. stat->ino = CIFS_I(dentry->d_inode)->uniqueid;
  1324. }
  1325. return err;
  1326. }
  1327. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1328. {
  1329. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1330. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1331. struct page *page;
  1332. int rc = 0;
  1333. page = grab_cache_page(mapping, index);
  1334. if (!page)
  1335. return -ENOMEM;
  1336. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  1337. unlock_page(page);
  1338. page_cache_release(page);
  1339. return rc;
  1340. }
  1341. static int cifs_vmtruncate(struct inode *inode, loff_t offset)
  1342. {
  1343. loff_t oldsize;
  1344. int err;
  1345. spin_lock(&inode->i_lock);
  1346. err = inode_newsize_ok(inode, offset);
  1347. if (err) {
  1348. spin_unlock(&inode->i_lock);
  1349. goto out;
  1350. }
  1351. oldsize = inode->i_size;
  1352. i_size_write(inode, offset);
  1353. spin_unlock(&inode->i_lock);
  1354. truncate_pagecache(inode, oldsize, offset);
  1355. if (inode->i_op->truncate)
  1356. inode->i_op->truncate(inode);
  1357. out:
  1358. return err;
  1359. }
  1360. static int
  1361. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1362. int xid, char *full_path)
  1363. {
  1364. int rc;
  1365. struct cifsFileInfo *open_file;
  1366. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1367. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1368. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1369. /*
  1370. * To avoid spurious oplock breaks from server, in the case of
  1371. * inodes that we already have open, avoid doing path based
  1372. * setting of file size if we can do it by handle.
  1373. * This keeps our caching token (oplock) and avoids timeouts
  1374. * when the local oplock break takes longer to flush
  1375. * writebehind data than the SMB timeout for the SetPathInfo
  1376. * request would allow
  1377. */
  1378. open_file = find_writable_file(cifsInode);
  1379. if (open_file) {
  1380. __u16 nfid = open_file->netfid;
  1381. __u32 npid = open_file->pid;
  1382. rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
  1383. npid, false);
  1384. cifsFileInfo_put(open_file);
  1385. cFYI(1, ("SetFSize for attrs rc = %d", rc));
  1386. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1387. unsigned int bytes_written;
  1388. rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
  1389. &bytes_written, NULL, NULL, 1);
  1390. cFYI(1, ("Wrt seteof rc %d", rc));
  1391. }
  1392. } else
  1393. rc = -EINVAL;
  1394. if (rc != 0) {
  1395. /* Set file size by pathname rather than by handle
  1396. either because no valid, writeable file handle for
  1397. it was found or because there was an error setting
  1398. it by handle */
  1399. rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
  1400. false, cifs_sb->local_nls,
  1401. cifs_sb->mnt_cifs_flags &
  1402. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1403. cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
  1404. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1405. __u16 netfid;
  1406. int oplock = 0;
  1407. rc = SMBLegacyOpen(xid, pTcon, full_path,
  1408. FILE_OPEN, GENERIC_WRITE,
  1409. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  1410. cifs_sb->local_nls,
  1411. cifs_sb->mnt_cifs_flags &
  1412. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1413. if (rc == 0) {
  1414. unsigned int bytes_written;
  1415. rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
  1416. attrs->ia_size,
  1417. &bytes_written, NULL,
  1418. NULL, 1);
  1419. cFYI(1, ("wrt seteof rc %d", rc));
  1420. CIFSSMBClose(xid, pTcon, netfid);
  1421. }
  1422. }
  1423. }
  1424. if (rc == 0) {
  1425. cifsInode->server_eof = attrs->ia_size;
  1426. rc = cifs_vmtruncate(inode, attrs->ia_size);
  1427. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1428. }
  1429. return rc;
  1430. }
  1431. static int
  1432. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1433. {
  1434. int rc;
  1435. int xid;
  1436. char *full_path = NULL;
  1437. struct inode *inode = direntry->d_inode;
  1438. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1439. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1440. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1441. struct cifs_unix_set_info_args *args = NULL;
  1442. struct cifsFileInfo *open_file;
  1443. cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
  1444. direntry->d_name.name, attrs->ia_valid));
  1445. xid = GetXid();
  1446. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1447. /* check if we have permission to change attrs */
  1448. rc = inode_change_ok(inode, attrs);
  1449. if (rc < 0)
  1450. goto out;
  1451. else
  1452. rc = 0;
  1453. }
  1454. full_path = build_path_from_dentry(direntry);
  1455. if (full_path == NULL) {
  1456. rc = -ENOMEM;
  1457. goto out;
  1458. }
  1459. /*
  1460. * Attempt to flush data before changing attributes. We need to do
  1461. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1462. * ownership or mode then we may also need to do this. Here, we take
  1463. * the safe way out and just do the flush on all setattr requests. If
  1464. * the flush returns error, store it to report later and continue.
  1465. *
  1466. * BB: This should be smarter. Why bother flushing pages that
  1467. * will be truncated anyway? Also, should we error out here if
  1468. * the flush returns error?
  1469. */
  1470. rc = filemap_write_and_wait(inode->i_mapping);
  1471. if (rc != 0) {
  1472. cifsInode->write_behind_rc = rc;
  1473. rc = 0;
  1474. }
  1475. if (attrs->ia_valid & ATTR_SIZE) {
  1476. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1477. if (rc != 0)
  1478. goto out;
  1479. }
  1480. /* skip mode change if it's just for clearing setuid/setgid */
  1481. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1482. attrs->ia_valid &= ~ATTR_MODE;
  1483. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1484. if (args == NULL) {
  1485. rc = -ENOMEM;
  1486. goto out;
  1487. }
  1488. /* set up the struct */
  1489. if (attrs->ia_valid & ATTR_MODE)
  1490. args->mode = attrs->ia_mode;
  1491. else
  1492. args->mode = NO_CHANGE_64;
  1493. if (attrs->ia_valid & ATTR_UID)
  1494. args->uid = attrs->ia_uid;
  1495. else
  1496. args->uid = NO_CHANGE_64;
  1497. if (attrs->ia_valid & ATTR_GID)
  1498. args->gid = attrs->ia_gid;
  1499. else
  1500. args->gid = NO_CHANGE_64;
  1501. if (attrs->ia_valid & ATTR_ATIME)
  1502. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  1503. else
  1504. args->atime = NO_CHANGE_64;
  1505. if (attrs->ia_valid & ATTR_MTIME)
  1506. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  1507. else
  1508. args->mtime = NO_CHANGE_64;
  1509. if (attrs->ia_valid & ATTR_CTIME)
  1510. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  1511. else
  1512. args->ctime = NO_CHANGE_64;
  1513. args->device = 0;
  1514. open_file = find_writable_file(cifsInode);
  1515. if (open_file) {
  1516. u16 nfid = open_file->netfid;
  1517. u32 npid = open_file->pid;
  1518. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  1519. cifsFileInfo_put(open_file);
  1520. } else {
  1521. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  1522. cifs_sb->local_nls,
  1523. cifs_sb->mnt_cifs_flags &
  1524. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1525. }
  1526. if (!rc) {
  1527. rc = inode_setattr(inode, attrs);
  1528. /* force revalidate when any of these times are set since some
  1529. of the fs types (eg ext3, fat) do not have fine enough
  1530. time granularity to match protocol, and we do not have a
  1531. a way (yet) to query the server fs's time granularity (and
  1532. whether it rounds times down).
  1533. */
  1534. if (!rc && (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME)))
  1535. cifsInode->time = 0;
  1536. }
  1537. out:
  1538. kfree(args);
  1539. kfree(full_path);
  1540. FreeXid(xid);
  1541. return rc;
  1542. }
  1543. static int
  1544. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  1545. {
  1546. int xid;
  1547. struct inode *inode = direntry->d_inode;
  1548. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1549. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1550. char *full_path = NULL;
  1551. int rc = -EACCES;
  1552. __u32 dosattr = 0;
  1553. __u64 mode = NO_CHANGE_64;
  1554. xid = GetXid();
  1555. cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
  1556. direntry->d_name.name, attrs->ia_valid));
  1557. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1558. /* check if we have permission to change attrs */
  1559. rc = inode_change_ok(inode, attrs);
  1560. if (rc < 0) {
  1561. FreeXid(xid);
  1562. return rc;
  1563. } else
  1564. rc = 0;
  1565. }
  1566. full_path = build_path_from_dentry(direntry);
  1567. if (full_path == NULL) {
  1568. rc = -ENOMEM;
  1569. FreeXid(xid);
  1570. return rc;
  1571. }
  1572. /*
  1573. * Attempt to flush data before changing attributes. We need to do
  1574. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1575. * ownership or mode then we may also need to do this. Here, we take
  1576. * the safe way out and just do the flush on all setattr requests. If
  1577. * the flush returns error, store it to report later and continue.
  1578. *
  1579. * BB: This should be smarter. Why bother flushing pages that
  1580. * will be truncated anyway? Also, should we error out here if
  1581. * the flush returns error?
  1582. */
  1583. rc = filemap_write_and_wait(inode->i_mapping);
  1584. if (rc != 0) {
  1585. cifsInode->write_behind_rc = rc;
  1586. rc = 0;
  1587. }
  1588. if (attrs->ia_valid & ATTR_SIZE) {
  1589. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1590. if (rc != 0)
  1591. goto cifs_setattr_exit;
  1592. }
  1593. /*
  1594. * Without unix extensions we can't send ownership changes to the
  1595. * server, so silently ignore them. This is consistent with how
  1596. * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
  1597. * CIFSACL support + proper Windows to Unix idmapping, we may be
  1598. * able to support this in the future.
  1599. */
  1600. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  1601. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  1602. /* skip mode change if it's just for clearing setuid/setgid */
  1603. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1604. attrs->ia_valid &= ~ATTR_MODE;
  1605. if (attrs->ia_valid & ATTR_MODE) {
  1606. cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
  1607. mode = attrs->ia_mode;
  1608. }
  1609. if (attrs->ia_valid & ATTR_MODE) {
  1610. rc = 0;
  1611. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1612. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  1613. rc = mode_to_acl(inode, full_path, mode);
  1614. else
  1615. #endif
  1616. if (((mode & S_IWUGO) == 0) &&
  1617. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  1618. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  1619. /* fix up mode if we're not using dynperm */
  1620. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  1621. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  1622. } else if ((mode & S_IWUGO) &&
  1623. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  1624. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  1625. /* Attributes of 0 are ignored */
  1626. if (dosattr == 0)
  1627. dosattr |= ATTR_NORMAL;
  1628. /* reset local inode permissions to normal */
  1629. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1630. attrs->ia_mode &= ~(S_IALLUGO);
  1631. if (S_ISDIR(inode->i_mode))
  1632. attrs->ia_mode |=
  1633. cifs_sb->mnt_dir_mode;
  1634. else
  1635. attrs->ia_mode |=
  1636. cifs_sb->mnt_file_mode;
  1637. }
  1638. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1639. /* ignore mode change - ATTR_READONLY hasn't changed */
  1640. attrs->ia_valid &= ~ATTR_MODE;
  1641. }
  1642. }
  1643. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  1644. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  1645. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1646. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  1647. /* Even if error on time set, no sense failing the call if
  1648. the server would set the time to a reasonable value anyway,
  1649. and this check ensures that we are not being called from
  1650. sys_utimes in which case we ought to fail the call back to
  1651. the user when the server rejects the call */
  1652. if ((rc) && (attrs->ia_valid &
  1653. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1654. rc = 0;
  1655. }
  1656. /* do not need local check to inode_check_ok since the server does
  1657. that */
  1658. if (!rc)
  1659. rc = inode_setattr(inode, attrs);
  1660. cifs_setattr_exit:
  1661. kfree(full_path);
  1662. FreeXid(xid);
  1663. return rc;
  1664. }
  1665. int
  1666. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1667. {
  1668. struct inode *inode = direntry->d_inode;
  1669. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1670. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1671. if (pTcon->unix_ext)
  1672. return cifs_setattr_unix(direntry, attrs);
  1673. return cifs_setattr_nounix(direntry, attrs);
  1674. /* BB: add cifs_setattr_legacy for really old servers */
  1675. }
  1676. #if 0
  1677. void cifs_delete_inode(struct inode *inode)
  1678. {
  1679. cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
  1680. /* may have to add back in if and when safe distributed caching of
  1681. directories added e.g. via FindNotify */
  1682. }
  1683. #endif