inode.c 55 KB

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