inode.c 56 KB

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