inode.c 55 KB

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